944,162 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3970
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 10th, 2006
0

New task from Projects for the beginner:

Expand Post »
Unique random variables. I've tried modifying the code in varying ways, but nothing is working. The program won't add new numbers to replace the ones that were duplicates.

Python Syntax (Toggle Plain Text)
  1. import random
  2. b=0
  3. numbers=[]
  4. print "show 6 random unique integers from 1 to 50: "
  5. while b < 6:
  6. for k in range(6):
  7. k = random.randint(1,10)
  8. numbers.append(k)
  9. b=b+1
  10. setA = set(numbers)
  11. numbers = list(setA)
  12. print numbers

It's getting silly now.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

Click to Expand / Collapse  Quote originally posted by chris99 ...
Unique random variables. I've tried modifying the code in varying ways, but nothing is working. The program won't add new numbers to replace the ones that were duplicates.

Python Syntax (Toggle Plain Text)
  1. import random
  2. b=0
  3. numbers=[]
  4. print "show 6 random unique integers from 1 to 50: "
  5. while b < 6:
  6. for k in range(6):
  7. k = random.randint(1,10)
  8. numbers.append(k)
  9. b=b+1
  10. setA = set(numbers)
  11. numbers = list(setA)
  12. print numbers

It's getting silly now.
how about using random.sample()?
eg
Python Syntax (Toggle Plain Text)
  1. >>> import random
  2. >>> random.sample(range(0,50),6)
  3. [19, 39, 44, 13, 8, 31]
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

Thanks, but now I'm stuck on the next step, to allow the user to input their own numbers and compare them:

Python Syntax (Toggle Plain Text)
  1. import random
  2. r=[]
  3.  
  4. def randomness():
  5. print "show 6 random unique integers from 1 to 50: "
  6. sample1=random.sample(range(0,50),6)
  7. print sample1
  8. i=0
  9. while i != 6:
  10. for k in range(6):
  11. r = input('Type a random unique number from 1 - 50: ')
  12. i=i+1
  13. def startup():
  14. n=0
  15. randomness()
  16. while n !=6:
  17. if r == sample1:
  18. print "Exact Match!"
  19. else:
  20. print 'Try again'
  21. n=n+1
  22.  
  23. startup()
  24. if n == 6:
  25. print "no matches"
  26. else:
  27. print "congrats, it's a match"

This part is hard, although I think I have got the wrong idea.
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

Click to Expand / Collapse  Quote originally posted by chris99 ...
Thanks, but now I'm stuck on the next step, to allow the user to input their own numbers and compare them:

Python Syntax (Toggle Plain Text)
  1. import random
  2. r=[]
  3.  
  4. def randomness():
  5. print "show 6 random unique integers from 1 to 50: "
  6. sample1=random.sample(range(0,50),6)
  7. print sample1
  8. i=0
  9. while i != 6:
  10. for k in range(6):
  11. r = input('Type a random unique number from 1 - 50: ')
  12. i=i+1
  13. def startup():
  14. n=0
  15. randomness()
  16. while n !=6:
  17. if r == sample1:
  18. print "Exact Match!"
  19. else:
  20. print 'Try again'
  21. n=n+1
  22.  
  23. startup()
  24. if n == 6:
  25. print "no matches"
  26. else:
  27. print "congrats, it's a match"

This part is hard, although I think I have got the wrong idea.

you did not return values from randomness() , so in startup(), you can't use sample1.
anyway, what is it that you are trying to do?
do you need the user to guess all 6 random numbers?
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

I need the user to type 6 random, unique numbers 1-50, and for the program to come up with 6 random numbers in the same range. Then the program compares the lists, and if they are not the same, the program runs the rng again to create a new list that it compares with the input. If the lists are the same the program says so, and lists how many tries it took. Then it ends.

Moderator's note, chris is trying to solve one of the "projects for beginners" located at: http://www.daniweb.com/techtalkforum...250820-75.html
Thanks for your great help ghostdog74! These are not homework problems, so any constructive help to aid a beginners learning process is appreciated!
Last edited by vegaseat; Sep 10th, 2006 at 2:37 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

Just for learning purposes:
Python Syntax (Toggle Plain Text)
  1. # one alternative to random.sample(range(0,50), 6)
  2.  
  3. import random
  4.  
  5. print "show 6 random unique integers from 1 to 50:"
  6. numbers=[]
  7. while len(numbers) < 6:
  8. ri = random.randint(1,50)
  9. if ri not in numbers:
  10. numbers.append(ri)
  11.  
  12. print numbers
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Sep 10th, 2006
0

Re: New task from Projects for the beginner:

I would split user and computer creation of random numbers up like this:
Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. def computer_random():
  4. """let computer create a list of 6 unique random integers from 1 to 50"""
  5. ci = random.sample(range(1,50), 6)
  6. return ci
  7.  
  8. def user_random():
  9. """let user create a list of 6 unique random integers from 1 to 50"""
  10. print "Enter 6 random integers from 1 to 50:"
  11. ui = []
  12. while len(ui) < 6:
  13. print len(ui) + 1,
  14. i = input("--> ")
  15. # check if i is unique and has a value from 1 to 50
  16. # otherwise don't append, could also assure that i is an integer
  17. if (i not in ui) and (1 <= i <= 50):
  18. ui.append(i)
  19. return ui
  20.  
  21. comp_list = computer_random() # assign the returned list to variable comp_list
  22. print comp_list # test it
  23.  
  24. user_list = user_random()
  25. print user_list # test it
Function comp_random() will be called numerous times, each time you need to compare comp_list with user_list. Now you have to work on list compare function to find the matches, then count how many times 3 to 6matches have occured.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Sep 13th, 2006
0

Re: New task from Projects for the beginner:

Here is the current code. Thanks for your help. I've tried comparing each individual index after sorting both lists, so 1=1 2 != 2 3=3 etc
but there is a error: object "list" cannot be called:

Python Syntax (Toggle Plain Text)
  1. import random
  2. def computer_random():
  3. """Let computer create a list of 6 unique random ints from 1 to 50"""
  4. numbers=[]
  5. while len(numbers) < 6:
  6. ri = random.randint(1,50)
  7. if ri not in numbers:
  8. numbers.append(ri)
  9. return numbers
  10. def user_random():
  11. """Let user create a list of 6 unique random integers from 1 to 50"""
  12. print "Enter 6 random integers from 1 to 50:"
  13. ui = []
  14. while len(ui) < 6:
  15. print len(ui)+1,
  16. i = input("--> ")
  17. #check if i is unique and has a value from 1 to 50
  18. #otherwise don't append, could also assure that i is an integer
  19. if (i not in ui) and (1 <= i <= 50):
  20. ui.append(i)
  21. return ui
  22. comp_list = computer_random()
  23. print comp_list
  24. user_list = user_random()
  25. print user_list
  26. user_list.sort()
  27. comp_list.sort()
  28. match = 0
  29. index = 0
  30. while index != 6:
  31. if user_list(index) == comp_list(index):
  32. match = match+1
  33. index = index + 1
  34. else:
  35. index = index + 1
  36. print match
  37. if match < 3:
  38. print "Not alike"
  39. else:
  40. print match, "numbers are alike. Well done"

Where have I gone wrong?
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 13th, 2006
0

Re: New task from Projects for the beginner:

Click to Expand / Collapse  Quote originally posted by chris99 ...
Here is the current code. Thanks for your help. I've tried comparing each individual index after sorting both lists, so 1=1 2 != 2 3=3 etc
but there is a error: object "list" cannot be called:
Python Syntax (Toggle Plain Text)
  1. ....
  2. if user_list(index) == comp_list(index):
  3. ....
change the () to [] , it should be
Python Syntax (Toggle Plain Text)
  1. if user_list[index] == comp_list[index]:
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Sep 13th, 2006
0

Re: New task from Projects for the beginner:

Thanks, didn't notice that syntax issue! Now to make it repeat and count how many successful matches there are in 3 runs!
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python
Next Thread in Python Forum Timeline: File Saves





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC