python-2.7

Pygame: colliding rectangles with other rectangles in the same list

风流意气都作罢 提交于 2021-02-04 07:58:28
问题 I have a list of 10 drawn rectangles (referenced as cubes in my script) that are affected by gravity. I made a simple collision system for them to stop when they hit the ground. How can I make it so when 2 cubes collide they stop falling like they do with the ground? import pygame import time import random pygame.init() clock = pygame.time.Clock() wnx = 800 wny = 600 black = (0,0,0) grey = (75,75,75) white = (255,255,255) orange = (255,100,30) wn = pygame.display.set_mode((wnx, wny)) wn.fill

Function returns None

限于喜欢 提交于 2021-02-02 09:32:35
问题 After making an error then doing recursion i get None. def getplayerinput(): a = ["rock","paper","scissors"] plin = raw_input("Choose %s/%s/%s: " %(a[0], a[1], a[2])) print plin,'-first print' if plin not in a: print "Wrong input" getplayerinput() else: print plin,'-second print' return plin for i in range(0,11): print getplayerinput() If you input first 'rock' then 'cat' then 'paper' you will get a 'None'. 回答1: You do not return anything once the input is not valid. Do this instead: def

Function returns None

爱⌒轻易说出口 提交于 2021-02-02 09:31:09
问题 After making an error then doing recursion i get None. def getplayerinput(): a = ["rock","paper","scissors"] plin = raw_input("Choose %s/%s/%s: " %(a[0], a[1], a[2])) print plin,'-first print' if plin not in a: print "Wrong input" getplayerinput() else: print plin,'-second print' return plin for i in range(0,11): print getplayerinput() If you input first 'rock' then 'cat' then 'paper' you will get a 'None'. 回答1: You do not return anything once the input is not valid. Do this instead: def

How to get multiple max key values in a dictionary?

帅比萌擦擦* 提交于 2021-02-02 09:24:41
问题 Let's say I have a dictionary: data = {'a':1, 'b':2, 'c': 3, 'd': 3} I want to get the maximum value(s) in the dictionary. So far, I have been just doing: max(zip(data.values(), data.keys()))[1] but I'm aware that I could be missing another max value. What would be the most efficient way to approach this? 回答1: Based on your example, it seems like you're looking for the key(s) which map to the maximum value. You could use a list comprehension: [k for k, v in data.items() if v == max(data

How to get multiple max key values in a dictionary?

谁都会走 提交于 2021-02-02 09:23:11
问题 Let's say I have a dictionary: data = {'a':1, 'b':2, 'c': 3, 'd': 3} I want to get the maximum value(s) in the dictionary. So far, I have been just doing: max(zip(data.values(), data.keys()))[1] but I'm aware that I could be missing another max value. What would be the most efficient way to approach this? 回答1: Based on your example, it seems like you're looking for the key(s) which map to the maximum value. You could use a list comprehension: [k for k, v in data.items() if v == max(data

Restarting an optimisation with Pymoo

╄→гoц情女王★ 提交于 2021-01-29 22:25:57
问题 I'm trying to restart an optimisation in pymoo. I have a problem defined as: class myOptProb(Problem): """my body goes here""" algorithm = NSGA2(pop_size=24) problem = myOptProblem(opt_obj=dp_ptr, nvars=7, nobj=4, nconstr=0, lb=0.3 * np.ones(7), ub=0.7 * np.ones(7), parallelization=('threads', cpu_count(),)) res = minimize(problem, algorithm, ('n_gen', 100), seed=1, verbose=True) During the optimisation I write the design vectors and results to a .csv file. An example of design_vectors.csv is

Python Continue with execution in case of exception

淺唱寂寞╮ 提交于 2021-01-29 22:23:10
问题 I am trying to continue with my code eventhough exception is present. Just print the exception and continue with code. Below is sample : def mkdir(path): mypath = "./customers/"+path print(mypath) try: os.makedirs(mypath) except OSError as exc: if exc.errno == errno.EEXIST and os.path.isdir(mypath): pass if __name__ == '__main__': item = 'dev' mkdir(item) print("Done") But It never prints Done. Console OutPut ./customers/dev --------------------------------------------------------------------

Restarting an optimisation with Pymoo

≡放荡痞女 提交于 2021-01-29 22:21:44
问题 I'm trying to restart an optimisation in pymoo. I have a problem defined as: class myOptProb(Problem): """my body goes here""" algorithm = NSGA2(pop_size=24) problem = myOptProblem(opt_obj=dp_ptr, nvars=7, nobj=4, nconstr=0, lb=0.3 * np.ones(7), ub=0.7 * np.ones(7), parallelization=('threads', cpu_count(),)) res = minimize(problem, algorithm, ('n_gen', 100), seed=1, verbose=True) During the optimisation I write the design vectors and results to a .csv file. An example of design_vectors.csv is

Restarting an optimisation with Pymoo

霸气de小男生 提交于 2021-01-29 21:02:04
问题 I'm trying to restart an optimisation in pymoo. I have a problem defined as: class myOptProb(Problem): """my body goes here""" algorithm = NSGA2(pop_size=24) problem = myOptProblem(opt_obj=dp_ptr, nvars=7, nobj=4, nconstr=0, lb=0.3 * np.ones(7), ub=0.7 * np.ones(7), parallelization=('threads', cpu_count(),)) res = minimize(problem, algorithm, ('n_gen', 100), seed=1, verbose=True) During the optimisation I write the design vectors and results to a .csv file. An example of design_vectors.csv is

CGI - HTML to python

那年仲夏 提交于 2021-01-29 19:00:31
问题 I am following the tutorial CGI, when I try to execute the program #!C:/Python27/python.exe # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') print "Content-type:text/html\r\n\r\n" print "<html>" print "<head>" print "<title>Hello - Second CGI Program</title>" print "</head>" print "<body>" print "<h2>Hello %s %s</h2>" % (first