| | |
New task from Projects for the beginner:
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
Python Syntax (Toggle Plain Text)
import random def computer_random(): """Let computer create a list of 6 unique random ints from 1 to 50""" numbers=[] while len(numbers) < 6: ri = random.randint(1,50) if ri not in numbers: numbers.append(ri) return numbers def user_random(): """Let user create a list of 6 unique random integers from 1 to 50""" print "Enter 6 random integers from 1 to 50:" ui = [] while len(ui) < 6: print len(ui)+1, i = input("--> ") #check if i is unique and has a value from 1 to 50 #otherwise don't append, could also assure that i is an integer if (i not in ui) and (1 <= i <= 50): ui.append(i) return ui total_win = 0 current_win = 0 while current_win < 3: comp_list = computer_random() print comp_list user_list = user_random() user_list.sort() comp_list.sort() match = 0 index = 0 while index != 6: if user_list[index] == comp_list[index]: match = match+1 index = index+1 else: index = index+1 print match if match < 3: print "Not alike" else: print match, "numbers are alike. Well done" total_win=total_win+1 current_win = current_win + 1 print "You got ",total_win," out of ",current_win," matches"
Last edited by chris99; Sep 13th, 2006 at 5:41 am. Reason: Added text
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!
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
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:
Is this what you had in mind?
Python Syntax (Toggle Plain Text)
import random def computer_random(): """Let computer create a list of 6 unique random ints from 1 to 50""" numbers=[] while len(numbers) < 6: ri = random.randint(1,50) if ri not in numbers: numbers.append(ri) return numbers def user_random(): """Let user create a list of 6 unique random integers from 1 to 50""" print "Enter 6 random integers from 1 to 50:" ui = [] while len(ui) < 6: print len(ui)+1, i = input("--> ") #check if i is unique and has a value from 1 to 50 #otherwise don't append, could also assure that i is an integer if (i not in ui) and (1 <= i <= 50): ui.append(i) return ui total_win = 0 current_win = 0 user_list = user_random() while total_win < 1: comp_list = computer_random() print comp_list user_list.sort() comp_list.sort() match = 0 index = 0 while index != 6: if comp_list[index] in user_list: match = match+1 index = index+1 else: index = index+1 print match if total_win < 1: print "Not alike" else: print match, "numbers are alike. Well done" total_win=total_win+1 current_win = current_win + 1 print "You got ",total_win," out of ",current_win," matches"
Is this what you had in mind?
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 ...
Since you asked, Uschi Sallhofer was a student from Austria, absolutely witty and absolutely charming!
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 ...
Python Syntax (Toggle Plain Text)
import random def computer_random(): """Let computer create a list of 6 unique random ints from 1 to 50""" numbers=[] while len(numbers) < 6: ri = random.randint(1,50) if ri not in numbers: numbers.append(ri) return numbers def user_random(): """Let user create a list of 6 unique random integers from 1 to 50""" print "Enter 6 random integers from 1 to 50:" ui = [] while len(ui) < 6: print len(ui)+1, i = input("--> ") #check if i is unique and has a value from 1 to 50 #otherwise don't append, could also assure that i is an integer if (i not in ui) and (1 <= i <= 50): ui.append(i) return ui # initiate 3, 4, 5 or 6 match counters match3 = 0 match4 = 0 match5 = 0 match6 = 0 user_list = user_random() print "One moment please ..." tries = 100000 current_win = 0 while current_win < tries: comp_list = computer_random() #comp_list.sort() # not needed! match = 0 index = 0 # note: match += 1 is a short version of match = match + 1 while index != 6: if comp_list[index] in user_list: match += 1 index += 1 """ # for test only ... if match > 2: print current_win, comp_list, match """ if match == 3: match3 += 1 elif match == 4: match4 += 1 elif match == 5: match5 += 1 elif match == 6: match6 += 1 current_win += 1 print "You got the following matches out of %d tries" % (tries) print "3 matches = %d" % match3 print "4 matches = %d" % match4 print "5 matches = %d" % match5 print "6 matches = %d" % match6 """ typical result: You got the following matches out of 100000 tries 3 matches = 1671 4 matches = 89 5 matches = 3 6 matches = 0 """
Last edited by vegaseat; Sep 16th, 2006 at 3:57 am.
May 'the Google' be with you!
![]() |
Similar Threads
- Projects for the Beginner (Python)
- SETI@Home Director - Interviewed about Present & Future Projects (Geeks' Lounge)
- Beginner needs assistance with 'error checking' program (C++)
Other Threads in the Python Forum
- Previous Thread: python
- Next Thread: File Saves
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






