Reputation: 1621
I start learning Python by myself, I googled around but I can not find the solution,
This is practice of lottery code and I would like to ask User couple of question. It is still practicing.
I try to build "add_players()" part.
So, My code is right now
user_players = set()
lottery_numbers = {13, 21, 22, 5, 8}
def menu():
user_input = input("ADD,SHOW,ROLL or QUIT ")
user_input=user_input.upper()
while user_input != 'QUIT':
if user_input == 'ADD':
add_players()
#elif user_input == 'SHOW':
# show_players()
elif user_input == 'ROLL':
roll()
else:
print('Done bye')
user_input = input("ADD,SHOW,ROLL or QUIT ")
def add_players():
name_input = input('Name?: ')
numbers_input = int(input('number?: '))
new_users = user_players.add(
{
'name': name_input,
'numbers': numbers_input
}
)
def roll():
for i in new_users:
Matched1 = i['numbers'].intersection(lottery_numbers)
print("{} matched {} ".format(i['name'], Matched1))
menu()
So when a user chose "ADD",
I would like to add values to my SET
which is this logic
def add_players():
name_input = input('Name?: ')
numbers_input = int(input('number?: '))
new_users = user_players.add(
{
'name': name_input,
'numbers': numbers_input
}
)
for example,
John 1,2,3,4,5
I know I should use add function, but
I am missing this part
new_users = user_players.add(
{
'name': name_input,
'numbers': numbers_input
}
)
How can I change my code to add players and their numbers? so when user choose "ROLL", It shows like "John matched 5"
Upvotes: 0
Views: 56
Reputation: 46
{
'name': name_input,
'numbers': numbers_input
}
cannot be added to a set since a dict is not hashable. Try using a data structure that is hashable like tuple to suit your case.
For example,
new_users = user_players.add((name_input, numbers_input))
Also for your roll function, you refer to new_users
which aren't accessible in the function. new_users
you declared in add_players
function only lives inside that function. So you'd want to declare it else where, like just below user_players
on the top and add to it in the functions as you go.
Upvotes: 3