Reputation: 11
I have a problem with my logic. I'm currently reading python crash course, one of the exercises asks to model a lottery ticket and to see how many tries it took. The problem is I can leave it for days without winning....
import random
a_list = ('33','18','27','5','4','12')
winner_ticket = ('33','18','27')
run = 0
while True:
run += 1
if random.sample(a_list,3) == winner_ticket:
print(f"Your ticket is a winner with the numbers d{winner_ticket}")
break
if run > 50000:
break
else:
print(f"This is try number: {run}")
Upvotes: 0
Views: 1435
Reputation: 77
Your problem is that random.sample()
returns a list, but winner_ticket
is a tuple. So either make winner ticket a list:
winner_ticket = ['33','18','27']
...or cast random.sample()
to a tuple:
if tuple(random.sample(a_list,3)) == winner_ticket:
Upvotes: 0
Reputation: 92440
random.sample
returns a list. You are comparing a tuple.
This is False
:
('33', '5', '4') == ['33', '5', '4']
Try testing with a list instead.
winner_ticket = ['33','18','27']
Upvotes: 2
Reputation: 2328
This is clearly coursework so I'll provide some advice.
Your winner_ticket
is a tuple. random.sample()
returns a list. You cannot directly compare the two as they are not equal. You will have to check if each element is within the winning tuple.
Furthermore, you should note that the lists [1, 2, 3] does not equal the list [1, 3, 2]. random.sample()
can return the list elements in any order. One way you can try to get past this is to sort the winner_ticket
(after changing it to a list as a tuple is immutable) and sorting the random.sample()
If you follow the sorting method, then you can compare the two lists winner_ticket
and random.sample()
and get a valid result.
Upvotes: 1