Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Someone help please ???
#1
I'm trying to make a lottery ticket simulator but i have one issue...
i want the program not to print only Names, but surnames as well right after the names...
But it gives me this error: 

Error:
Traceback (most recent call last):   File "C:\Users\spale\Desktop\SandBox.py", line 8, in <module>     Winners1 = random.choice(Names,Surnames) TypeError: choice() takes 2 positional arguments but 3 were given
How do i fix this ???

import random

print("\nWinners of todays lottery are...")

Names = ["Adolf","Tomas","Ben","Daniel","Alex","Samantha","Nicola","Angel","Blake","Guy","John","David","Julia","Romeo","Elizabeth","Jasmin","Adam","Eva","Sonia","Malika","Trump"]
Surnames = ["Hitler","Spalek","Weird","Rowland","Frosbrook","Chandler","Smith","Who","Jones","Somebody","English","Okwiet","Trump","Donaldson","Jackson","Minerson","Slavik"]

Winners1 = random.choice(Names,Surnames)
Winners2 = random.choice(Names,Surnames)
Winners3 = random.choice(Names,Surnames)

def Winners():
    print(Winners1)
    print(Winners2)
    print(Winners3)

Winners()
Thank you for taking your time to help me sort out the issue!
i Do really appreciate it! Thank you :P
Reply
#2
You could either do this:

Winners1 = random.choice(Names) + random.choice(Surnames)
or

def Winners():
    print("Winner 1: {} {}".format(random.choice(Names), random.choice(Surnames)))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
or
winner = random.choice(zip(Names,Surnames))
print('Winner: {}'.format(' '.join(winner)))
however it's better to use different data container, e.g. dict, named tupple, etc. rather than two separate lists.
Reply
#4
(May-16-2017, 08:32 PM)buran Wrote: or
winner = random.choice(zip(Names,Surnames))
print('Winner: {}'.format(' '.join(winner)))
however it's better to use different data container, e.g. dict, named tupple, etc. rather than two separate lists.

random.choice() does not work on zip in python3 (no len defined), so converting it could be necessary.

Sparkz' and yours solutions are rather different, as first gives "unpaired" names, while second "paired" ones.
Reply
#5
@zivoni: nice catch. It is  my mistake, probably I tested it only with python2. For the sake of completeness So the python3 code would be

winner = random.choice(list(zip(Names,Surnames)))
print('Winner: {}'.format(' '.join(winner)))
The recommendation to use different data structure still stands
Reply
#6
Thank you everyone for answering my question ! :P
It helped a lot...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020