onyshkov
onyshkov

Reputation: 5

Create program that must created game LOTTO

import random
from random import sample

List1 is created by the user.

list1=[]

your_number1= int(input("Input 1 number: "))
list1.append(your_number1)

your_number2= int(input("Input 2 number: "))
list1.append(your_number2)

your_number3= int(input("Input 3 number: "))
list1.append(your_number3)

your_number4= int(input("Input 4 number: "))
list1.append(your_number4)

your_number5= int(input("Input 5 number: "))
list1.append(your_number5)

your_number6= int(input("Input 6 number: "))
list1.append(your_number6)



while(1):
  list2 = range(1, 50)
  list2 = random.sample(list2, 6)

It's list2 created by random

  for pair in zip(list1, list2):
      if pair[0] ==  pair[1]:
        print("You win")
        break
      else:
        print("You lose")

This FOR must compare 2 lists

All PROGRAM IS ------- Write a program that takes 6 numbers from the pool of 49 from the user. The program is to consider a set of unique six numbers. Then the program draws until it draws the user-selected list of numbers. The program gives after how many years and how many days the user would draw numbers (e.g. need x years and x days). We assume that the draws take place every day (one draw for one day). *

Upvotes: 0

Views: 74

Answers (2)

RafalS
RafalS

Reputation: 6344

import random

pool = list(range(1, 50))
bet = set(random.sample(pool, 6))
print(f"Random bet: {bet}")
days = 0
while set(random.sample(pool, 6)) != bet:
    days += 1
print(f"You would have to try {days} days")

EDIT: the previous version was taking too long. I reimplemented it with numpy:

import numpy as np

pool = np.arange(1, 50, dtype=np.int8)
bet = np.sort(np.random.choice(pool, (6,)))
batch_size = 10 ** 6
tries = 0
while True:
    draws = np.sort(np.random.choice(pool, size=(batch_size, 6)))
    bingo = np.argwhere(np.all(draws - bet == 0, axis=1))
    if bingo.shape == (1, 1):
        print("It took", tries + bingo.flat[0])
        break
    tries += batch_size

Upvotes: 0

Miguel
Miguel

Reputation: 1355

You can do:

import random
from random import sample
from collections import Counter

list1=[]
for i in range(1,7): # no need to repeat 6 times
     list1.append(input(f"Input {i} number: "))

attempts = 0
numbers = range(1,50)
while(True):
    list2 = random.sample(numbers, 6)
    attempts += 1
    if Counter(list1) == Counter(list2): # counter will make a comparison by counting how many times each number shows up in the list.
        print(f"You win after {attempts}")
        break


Upvotes: 1

Related Questions