Python Forum
what am i doing wrong???
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what am i doing wrong???
#1
Q. Lottery Analysis: You can use a loop to see how hard it might be to win the kind of lottery you just modeled. Make a list or tuple called my_ticket. Write a loop that keeps pulling numbers until your ticket wins. Print a message reporting how many times the loop had to run to give you a winning ticket.

why is it not working??


My code

from random import choice
list1 = [7, 'z', 10, 3, 4, 1, 'a', 6, 'c', 'b', 5, 8, 'j', 9, 2]

num1 = (choice(list1))
num2 = (choice(list1))
num3 = (choice(list1))
num4 = (choice(list1))

print(f"Any letter matching {num1, num2, num3, num4} these numbers will win the lottery.")

my_ticket = [0]
for num in my_ticket:
    num = choice(list1)
    my_ticket.append(num)
    if num1 and num2 and num3 and num4 in my_ticket:
        print('You have won the lottery.')
        break
print(my_ticket)
Larz60+ write Aug-09-2021, 08:01 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Is this what you're trying to do?
from random import randrange

list1 = [7, 'z', 10, 3, 4, 1, 'a', 6, 'c', 'b', 5, 8, 'j', 9, 2]

listlen = len(list1)

# num1 = (choice(list1))
# num2 = (choice(list1))
# num3 = (choice(list1))
# num4 = (choice(list1))
 
num1 = list1[randrange(listlen)]
num2 = list1[randrange(listlen)]
num3 = list1[randrange(listlen)]
num4 = list1[randrange(listlen)]


print(f"Any letter matching {num1, num2, num3, num4} these numbers will win the lottery.")
 
my_ticket = []
for num in range(4):
    # num = choice(list1)
    num1 = list1[randrange(listlen)]
    my_ticket.append(num1)

if num1 in my_ticket and num2 in my_ticket and num3 in my_ticket and num4 in my_ticket:
    print('You have won the lottery.')

print(f"my_ticket: {my_ticket}")
test:
Output:
Any letter matching (6, 6, 'c', 3) these numbers will win the lottery. my_ticket: [9, 6, 5, 'b']
Reply
#3
There are no duplicate numbers in a lottery, except possibly a special number like the power ball. But in general you don't have to worry about duplicate numbers, and you should not allow duplicates in a lottery ticket. The way you are using choice does not guarantee a valid lottery ticket.

Order doesn't matter in a lottery. If your lottery ticket contains the winning numbers, you win regardless of the order the balls were drawn.

Using these two pieces of information it appears that set logic is a good fit for solving this problem. In particular the issubset() method.
import random

all_choices = [7, 'z', 10, 3, 4, 1, 'a', 6, 'c', 'b', 5, 8, 'j', 9, 2]

random_sample = random.sample(all_choices, k=len(all_choices))
lottery_ticket = set(random.sample(all_choices, k=4))

for i in range(4, len(random_sample)):
    if lottery_ticket.issubset(random_sample[:i]):
        print(lottery_ticket, random_sample[:i])
        break
This code uses random.sample() to draw values from the all_choices list. sample() works like drawing cards from a deck. Ones a value is selected, it cannot be selected again. This meets our requirement that all numbers in a lottery ticket are unique.

random_sample = random.sample(all_choices, k=len(all_choices)) shuffles the list of lottery numbers. I could also use random.shuffle() to do this. The only reason I chose random.sample() is I was already using it for the lottery ticket (which could also be generated using shuffle()).

To determine how many numbers are drawn before you have a winning lottery ticket all you need to do is check if lottery_ticket is a subset of an increasingly larger slice of random_sample. We start with the smallest possible winning combo and iterate up to the full set of possible lottery numbers. Stop when the subset condition is met and print the random_choices subset.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong? Or is the question setup wrong? musicjoeyoung 3 4,028 May-18-2020, 03:38 PM
Last Post: musicjoeyoung

Forum Jump:

User Panel Messages

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