| | |
New task from Projects for the beginner:
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
It's getting silly now.
Python Syntax (Toggle Plain Text)
import random b=0 numbers=[] print "show 6 random unique integers from 1 to 50: " while b < 6: for k in range(6): k = random.randint(1,10) numbers.append(k) b=b+1 setA = set(numbers) numbers = list(setA) print numbers
It's getting silly now.
•
•
Join Date: Apr 2006
Posts: 154
Reputation:
Solved Threads: 40
•
•
•
•
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)
import random b=0 numbers=[] print "show 6 random unique integers from 1 to 50: " while b < 6: for k in range(6): k = random.randint(1,10) numbers.append(k) b=b+1 setA = set(numbers) numbers = list(setA) print numbers
It's getting silly now.
eg
Python Syntax (Toggle Plain Text)
>>> import random >>> random.sample(range(0,50),6) [19, 39, 44, 13, 8, 31]
Thanks, but now I'm stuck on the next step, to allow the user to input their own numbers and compare them:
This part is hard, although I think I have got the wrong idea.
Python Syntax (Toggle Plain Text)
import random r=[] def randomness(): print "show 6 random unique integers from 1 to 50: " sample1=random.sample(range(0,50),6) print sample1 i=0 while i != 6: for k in range(6): r = input('Type a random unique number from 1 - 50: ') i=i+1 def startup(): n=0 randomness() while n !=6: if r == sample1: print "Exact Match!" else: print 'Try again' n=n+1 startup() if n == 6: print "no matches" else: print "congrats, it's a match"
This part is hard, although I think I have got the wrong idea.
•
•
Join Date: Apr 2006
Posts: 154
Reputation:
Solved Threads: 40
•
•
•
•
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)
import random r=[] def randomness(): print "show 6 random unique integers from 1 to 50: " sample1=random.sample(range(0,50),6) print sample1 i=0 while i != 6: for k in range(6): r = input('Type a random unique number from 1 - 50: ') i=i+1 def startup(): n=0 randomness() while n !=6: if r == sample1: print "Exact Match!" else: print 'Try again' n=n+1 startup() if n == 6: print "no matches" else: 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?
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!
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.
Just for learning purposes:
Python Syntax (Toggle Plain Text)
# one alternative to random.sample(range(0,50), 6) import random print "show 6 random unique integers from 1 to 50:" numbers=[] while len(numbers) < 6: ri = random.randint(1,50) if ri not in numbers: numbers.append(ri) print numbers
I would split user and computer creation of random numbers up like this:
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.
Python Syntax (Toggle Plain Text)
import random def computer_random(): """let computer create a list of 6 unique random integers from 1 to 50""" ci = random.sample(range(1,50), 6) return ci 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 comp_list = computer_random() # assign the returned list to variable comp_list print comp_list # test it user_list = user_random() print user_list # test it
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:
Where have I gone wrong?
but there is a error: object "list" cannot be called:
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 comp_list = computer_random() print comp_list user_list = user_random() print user_list 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"
Where have I gone wrong?
•
•
Join Date: Apr 2006
Posts: 154
Reputation:
Solved Threads: 40
•
•
•
•
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)
.... if user_list(index) == comp_list(index): ....
Python Syntax (Toggle Plain Text)
if user_list[index] == comp_list[index]:
![]() |
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
Views: 3407 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






