user2908101
user2908101

Reputation: 99

while loop isn't working

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.

Upvotes: 0

Views: 517

Answers (2)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

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

Upvotes: 0

Lei
Lei

Reputation: 432

a = a+1 should be what you want.

Upvotes: 2

Related Questions