0

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.

asked Oct 24, 2013 at 23:25
6
  • randint() includes the endpoint, so you get random values between 1 and 366. Use randrange() if you expect the end point not to be included. Commented Oct 24, 2013 at 23:26
  • Your local names do not match. my is not the same as mySet. Commented Oct 24, 2013 at 23:27
  • The answer was solved. I have to wait 12 minutes to accept it. Thanks guys Commented Oct 24, 2013 at 23:28
  • @MartijnPieters do you think you could help me with the 2nd problem? Commented Oct 24, 2013 at 23:40
  • You should not add new problems to an existing, answered question; I see you now added a new question; that's the right thing to do. Commented Oct 24, 2013 at 23:50

2 Answers 2

2

a = a+1 should be what you want.

answered Oct 24, 2013 at 23:27
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Oct 24, 2013 at 23:29

1 Comment

Hey, do you know how I could solve the second part to my problem @Aswin Murugesh

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.