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.

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.

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.

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.

how about using random.sample()?
eg

>>> 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:

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.

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

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/techtalkforums/post250820-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!

Just for learning purposes:

# 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:

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

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.

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:

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?

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:

....
    if user_list(index) == comp_list(index):
  ....

change the () to [] , it should be

if user_list[index] == comp_list[index]:

Thanks, didn't notice that syntax issue! Now to make it repeat and count how many successful matches there are in 3 runs!

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.

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"

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!

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:

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 ...

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
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
"""

Since you asked, Uschi Sallhofer was a student from Austria, absolutely witty and absolutely charming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.