Can't get my for loops to work

248 Views Asked by At

Okay, so I'm trying to have the program do three steps...

1: choose a number between (m,n) which are parameters being passed in (Set to variable "repeat")

2: choose a random number between 0 and the variable "repeat" from step one. (Set to variable "o")

3: subract "o" from "repeat" and set that result to variable "p"

thus I get a number (lets say 100)

then a random number from 0-100 (lets say 40)

and then I get the difference of 100-40 (60...)

I then want the program to run a for loop "o" (40) times and another for loop "p" (60) times...

the code for the for loops section looks like this (keep in mind there is more code before this... It just doesn't really pertain to this question:

def randomStars(pic,m,n):

  repeat=random.randint(200,300)
  o=random.randint(0,repeat)
  p=repeat-o

  for i in o:
    star(pic,x,y)
  for j in p:
    largeStar(pic,x,y)
  show(pic)

What's happening is I'm getting an error message on the line: for i in o: that says "iteration over non-sequence inappropriate argument type

I've also added print statements after the 3 variables are set and they are working... ex.1 repeat=230; o=103; p=127 ex.2 repeat=221; o=72; p=149

and then I immediately try to get the for loop to run "o"number of times and I get the above error message... I don't see how it is a non-sequence. But perhaps I'm simply not understanding the definition of a sequence

1

There are 1 best solutions below

0
On

o and p are integers. For for loops you need something that is iterable. I thing you can change it to:

for i in range(o):

This is range() documentation for Python 2.x