python 3.x - for loop to shorten code -
x-axis increases + 100 there way shorten code using loop using python 3
def peasinapod(): win=graphwin(100,500) peas=eval(input("how many peas? ")) if peas == 5: p=circle(point(50,100),50) p2=circle(point(150,100),50) p3=circle(point(250, 100),50) p4=circle(point(350,100),50) p5=circle(point(450,100),50) p.draw(win) p2.draw(win) p3.draw(win) p4.draw(win) p5.draw(win)
edit
you asked shortest, right?
def peasinapod(): win = graphwin(100,500) list(map(lambda p: p.draw(win), [circle(point((i*100)+50,100),50) in range(int(input('how many peas? ')))]))
you need list
execute lambda
.
original answer:
something this?
def peasinapod(): win = graphwin(100,500) peas = eval(input('how many peas? ')) # use safer eval in range(peas): p = circle(point((i*100)+50,100),50) p.draw(win)
i'm assuming don't need reuse p*
variables elsewhere, , don't need store or refer later list of peas (this draws them). more spec provide, better answer you'll get! hope helps.
just fun, here's generator too! sorry, couldn't it...
def the_pod(how_many): p in range(how_many): yield circle(point((p*100)+50,100),50) def peasinapod(): win = graphwin(100,500) of_all_the_peas = input('how many peas? ') # raw_input python < 3 one_of_the_peas in the_pod(int(of_all_the_peas)): one_of_the_peas.draw(win)
this copies, pastes, , executes without dependencies. in case you're after infinite generator forces people have infinity peas.
def the_pod(): p = 0 while true: yield (p*100)+50 p += 1 def peasinapod(): print('you may have peas. well. x-coordinate.') one_of_the_peas in the_pod(): print(one_of_the_peas) peasinapod()
i'm off pea soup. thanks!
Comments
Post a Comment