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

Re: New task from Projects for the beginner:

 
0
  #11
Sep 13th, 2006
Just finished the program. It successfully runs three times and prints the amount of matches out of 3! I printed comp_list for debugging purposes.

  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. total_win = 0
  23. current_win = 0
  24. while current_win < 3:
  25. comp_list = computer_random()
  26. print comp_list
  27. user_list = user_random()
  28. user_list.sort()
  29. comp_list.sort()
  30. match = 0
  31. index = 0
  32. while index != 6:
  33. if user_list[index] == comp_list[index]:
  34. match = match+1
  35. index = index+1
  36. else:
  37. index = index+1
  38. print match
  39. if match < 3:
  40. print "Not alike"
  41. else:
  42. print match, "numbers are alike. Well done"
  43. total_win=total_win+1
  44. current_win = current_win + 1
  45. print "You got ",total_win," out of ",current_win," matches"
Last edited by chris99; Sep 13th, 2006 at 5:41 am. Reason: Added text
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: New task from Projects for the beginner:

 
0
  #12
Sep 13th, 2006
Nice code so far!
It would be easier on the user, if you only ask the user numbers to be entered once and then run the computer numbers against that! I like your compare, but there is a flaw. Lists [1,2,3,4,5,6] and [1,3,4,5,6,7] should have 5 matches, your compare would only show 1 match. I got to have time to look at it closer. To segregate 3, 4, 5 or 6 matches you just establish a counter for each. You might have to go to a million plus tries to get 6 matches!
Last edited by Ene Uran; Sep 13th, 2006 at 1:45 pm.
drink her pretty
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
  #13
Sep 15th, 2006
Here's the updated version. It went for about 100,000 tries without a match so I killed it, but if it had run long enough it might have matched 3 or more:

  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. total_win = 0
  23. current_win = 0
  24. user_list = user_random()
  25. while total_win < 1:
  26. comp_list = computer_random()
  27. print comp_list
  28. user_list.sort()
  29. comp_list.sort()
  30. match = 0
  31. index = 0
  32. while index != 6:
  33. if comp_list[index] in user_list:
  34. match = match+1
  35. index = index+1
  36. else:
  37. index = index+1
  38. print match
  39. if total_win < 1:
  40. print "Not alike"
  41. else:
  42. print match, "numbers are alike. Well done"
  43. total_win=total_win+1
  44. current_win = current_win + 1
  45. print "You got ",total_win," out of ",current_win," matches"

Is this what you had in mind?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,125
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: New task from Projects for the beginner:

 
0
  #14
Sep 16th, 2006
You are very close, very nice list compare by the way!

Avoid anything within the while loop that slows things down, like print statements. You also don't need any list sorting. Here would be my suggestion ...
  1. import random
  2.  
  3. def computer_random():
  4. """Let computer create a list of 6 unique random ints from 1 to 50"""
  5. numbers=[]
  6. while len(numbers) < 6:
  7. ri = random.randint(1,50)
  8. if ri not in numbers:
  9. numbers.append(ri)
  10. return numbers
  11.  
  12. def user_random():
  13. """Let user create a list of 6 unique random integers from 1 to 50"""
  14. print "Enter 6 random integers from 1 to 50:"
  15. ui = []
  16. while len(ui) < 6:
  17. print len(ui)+1,
  18. i = input("--> ")
  19. #check if i is unique and has a value from 1 to 50
  20. #otherwise don't append, could also assure that i is an integer
  21. if (i not in ui) and (1 <= i <= 50):
  22. ui.append(i)
  23. return ui
  24.  
  25. # initiate 3, 4, 5 or 6 match counters
  26. match3 = 0
  27. match4 = 0
  28. match5 = 0
  29. match6 = 0
  30.  
  31. user_list = user_random()
  32.  
  33. print "One moment please ..."
  34. tries = 100000
  35. current_win = 0
  36. while current_win < tries:
  37. comp_list = computer_random()
  38. #comp_list.sort() # not needed!
  39. match = 0
  40. index = 0
  41. # note: match += 1 is a short version of match = match + 1
  42. while index != 6:
  43. if comp_list[index] in user_list:
  44. match += 1
  45. index += 1
  46. """
  47. # for test only ...
  48. if match > 2:
  49. print current_win, comp_list, match
  50. """
  51. if match == 3:
  52. match3 += 1
  53. elif match == 4:
  54. match4 += 1
  55. elif match == 5:
  56. match5 += 1
  57. elif match == 6:
  58. match6 += 1
  59.  
  60. current_win += 1
  61.  
  62. print
  63. print "You got the following matches out of %d tries" % (tries)
  64. print "3 matches = %d" % match3
  65. print "4 matches = %d" % match4
  66. print "5 matches = %d" % match5
  67. print "6 matches = %d" % match6
  68.  
  69. """
  70. typical result:
  71. You got the following matches out of 100000 tries
  72. 3 matches = 1671
  73. 4 matches = 89
  74. 5 matches = 3
  75. 6 matches = 0
  76. """
Since you asked, Uschi Sallhofer was a student from Austria, absolutely witty and absolutely charming!
Last edited by vegaseat; Sep 16th, 2006 at 3:57 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC