mardi 9 août 2022

How do restrict the number of inputs and turn int into lists Python

I am trying to restrict the user input for the lottery tickets to seven characters and for it to only accept integers, when I use the int(input) it gives me a ValueError. When I remove the int before the input it works fine. I then go onto change the input into a list by using split method. How do I do restrict the inputs?

import random

number_choice = int(input("Input 7 lottery numbers between 1 and 100"))    
lottery_ticket = number_choice.split()          
print(type(lottery_ticket))        
random_nums = random.sample(range(1, 100), 7)  # generate a list of random numbers
print(lottery_ticket)
print(random_nums)
matches = set(lottery_ticket).intersection(random_nums)  # set.intersection matches the elements that are similar
print("The matching numbers are", matches)     


def matchingNumber():
    countMatches = sum(i in random_nums for i in lottery_ticket)
    print(countMatches)
    if countMatches == 2:
        print("You have won £5")
    elif countMatches == 3:
        print("you have won £20")
    elif countMatches == 4:
        print("you have won £40")
    elif countMatches == 5:
        print("you have won £100")
    elif countMatches == 6:
        print("you have won £10,000")
    elif countMatches == 7:
        print("you have won £1000000")
    else:
        print("You have won nothing!")


matchingNumber()



Aucun commentaire:

Enregistrer un commentaire