New task from Projects for the beginner:

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

New task from Projects for the beginner:

 
0
  #1
Sep 10th, 2006
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 154
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #2
Sep 10th, 2006
Originally Posted by chris99 View 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.

  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
  1. >>> import random
  2. >>> random.sample(range(0,50),6)
  3. [19, 39, 44, 13, 8, 31]
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #3
Sep 10th, 2006
Thanks, but now I'm stuck on the next step, to allow the user to input their own numbers and compare them:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 154
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #4
Sep 10th, 2006
Originally Posted by chris99 View Post
Thanks, but now I'm stuck on the next step, to allow the user to input their own numbers and compare them:

  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #5
Sep 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: New task from Projects for the beginner:

 
0
  #6
Sep 10th, 2006
Just for learning purposes:
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: New task from Projects for the beginner:

 
0
  #7
Sep 10th, 2006
I would split user and computer creation of random numbers up like this:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #8
Sep 13th, 2006
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 154
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #9
Sep 13th, 2006
Originally Posted by chris99 View Post
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:
  1. ....
  2. if user_list(index) == comp_list(index):
  3. ....
change the () to [] , it should be
  1. if user_list[index] == comp_list[index]:
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: New task from Projects for the beginner:

 
0
  #10
Sep 13th, 2006
Thanks, didn't notice that syntax issue! Now to make it repeat and count how many successful matches there are in 3 runs!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3407 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC