I would like to show the edited code that still doesn't work, I tried to do as advised by first converting numbers into integers and then converting the list to a set. But it keeps throwing the same error from the console: ValueError: invalid literal for int() with base 10: '1 3 5 6 7'
Here is the entire code:
lottery_numbers = [6, 7, 17, 5, 11, 25]
players = [
{"name": "Connor", "numbers": {6, 7, 17, 34, 11, 25}},
{"name": "Dave", "numbers": {3, 7, 35, 21, 5, 16}},
{"name": "Emily", "numbers": {12, 28, 4, 16, 17, 5}},
{"name": "Chris", "numbers": {34, 21, 3, 6, 13, 5}},
]
your_name = input("What is your name?")
given_numbers = input("Type any 6 numbers from 1 to 40: ")
your_numbers = [int(num) for num in given_numbers.split(',')]
your_combination = set(your_numbers)
players.append({"name": your_name, "numbers": your_combination})
print(your_combination)
numbers_matched = {}
prize_1 = 1000
prize_2 = 10000
prize_3 = 1000000
for player in players:
numbers_matched = player["numbers"].intersection(lottery_numbers)
print(f"{player['name']} has these numbers matched: {numbers_matched}")
if len(numbers_matched) > 5:
print(f"{player['name']} has won {prize_3} $")
elif len(numbers_matched) > 4:
print(f"{player['name']} has won {prize_2} $")
elif len(numbers_matched) > 3:
print(f"{player['name']} has won {prize_1} $")
enter code here
First off, there is no nested dictionary here. players
is a list. Specifically, a list of dictionaries. The numbers
key in each of these dictionaries is a set.
So, to add another dictionary to the list, you use .append
. Then to turn the list returned from .split
into a set, you use a set
cast.
Also, do not cast the comma separated number input to an int
int(input("Type any 6 numbers from 1 to 40: "))
How do you expect python to turn 1, 2, 3, 4, 5, 6, 7
to an int
? it wouldn't work. You can split this by ,
and cast each element to an int
We can do that using a comprehension
[int(x.strip()) for x in given_numbers.split(',')]
(Note, I'm using .strip
to get rid of any extra whitespace)
Or just directly turn that into a set
set(int(x.strip()) for x in given_numbers.split(','))
So this is how your full code could look-
your_name = input("What is your name?")
given_numbers = input("Type any 6 numbers from 1 to 40: ")
your_numbers = set(int(x.strip()) for x in given_numbers.split(','))
players.append({"name": your_name, "numbers": your_numbers})