2

x-axis increases by + 100 is there a way to shorten the code using for 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)
thecodesalim
  • 93
  • 1
  • 9
  • 1
    never use eval: [using-pythons-eval-vs-ast-literal-eval](http://stackoverflow.com/q/15197673/5644961), [eval_really_is_dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). In this case use `int(input(...))` – Copperfield Nov 06 '16 at 00:07
  • Would be super awesome if you could indicate whether or not any of the answers below (not necessarily mine of course) solved your problem :) – Eugene Nov 08 '16 at 02:16

3 Answers3

1

I assume you are looking for something along the lines of:

def peasInAPod():
    win=GraphWin(100,500)   
    peas=eval(input("how many peas? "))
    list_of_peas = [Circle(Point(50 + i * 100, 100),50) for i in range(0,peas)]
    for p in list_of_peas:
        p.draw(win)

EDIT The list comprehension can also be changed to:

list_of_peas = [Circle(Point(i, 100),50) for i in range(50,peas*100,100)]
UnholySheep
  • 3,967
  • 4
  • 19
  • 24
0

Yes, by using list comprehension:

def peasInAPod():
    win=GraphWin(100,500)   
    peas=eval(input("how many peas? "))
    if peas == 5:
        [Circle(Point(i, 100), 50).draw(win)
         for i in range(50, 550, 100)]
Philip Tzou
  • 5,926
  • 2
  • 18
  • 27
  • Why would you use a list comprehension here? You're not trying to build a list. – DSM Nov 05 '16 at 23:27
  • 2
    It's no shorter than just doing `for i in range(stuff): Circle(other_stuff).draw(win)` directly, and it builds and throws away an intermediate list for no reason. – DSM Nov 05 '16 at 23:31
  • You shouldn't use a functional construct, i.e. list comprehensions, for side-effects. Just use a for-loop. – juanpa.arrivillaga Nov 08 '16 at 02:16
0

EDIT

You asked for the shortest, right?

def peasInAPod():
    win = GraphWin(100,500)
    list(map(lambda p: p.draw(win), [Circle(Point((i*100)+50,100),50) for i in range(int(input('How many peas? ')))]))

You need the list to actually execute the lambda.

Original answer:

Something like this?

def peasInAPod():
    win = GraphWin(100,500)   
    peas = eval(input('How many peas? ')) # Use something safer than eval
    for i in range(peas):
        p = Circle(Point((i*100)+50,100),50)
        p.draw(win)

I'm assuming you don't need to reuse the p* variables elsewhere, and that you don't need to store or refer later to the list of peas (this just draws them). The more spec you provide, the better answer you'll get! Hope this helps.

Just for fun, here's a generator too! Sorry, I couldn't help it...

def the_pod(how_many):
    for 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 for Python < 3
    for one_of_the_peas in the_pod(int(of_all_the_peas)):
        one_of_the_peas.draw(win)

This copies, pastes, and executes without any dependencies. Just in case you're after an infinite generator that forces people to have infinity peas.

def the_pod():
    p = 0
    while True:
        yield (p*100)+50
        p += 1

def peasInAPod():  
    print('You may have all the peas. Well. Only their x-coordinate.')
    for one_of_the_peas in the_pod():
        print(one_of_the_peas)

peasInAPod()

I'm off to get some pea soup. Thanks!

Eugene
  • 1,539
  • 12
  • 20