I have a quick question. I have the following code...
def abc(c):
a = 1
my = set()
while a <= c:
b = randrange(1, 365)
my.add(b)
a = a + 1
print(my)
Now c is in my main function. c is a integer that the user is prompted for. For instance, if c = 10, then as long as a < 10 it will run the while loop and print out the set with 10 numbers randomly generated between 1 and 365. The only problem is that it's not printing out the set my correctly.
2 Answers 2
a = a+1 should be what you want.
Comments
a + 1 just increments the value of a, but does not store it anywhere. So, using a = a+1, will increment the value of a and update the value of a.
The second part: You are generating the random numbers and storing them in a set, and printing them at last. To print each and every element in the list, use:
for i in my:
print i
This will print each value in the set
randint()includes the endpoint, so you get random values between 1 and 366. Userandrange()if you expect the end point not to be included.myis not the same asmySet.