Reputation: 91
So, I'm trying to make a surprise program for my grandparents that generates a lottery ticket. (They're Spanish so most of the things in the code are.) If the user inputs a lottery name that is not in the choices variable., how can I make it so the code restarts?
Thanks in advance
import random
options = ("Bonoloto", "Primitiva", "Euromillón", "Gordo")
reintegro = random.sample(range(1,9), 1)
loteria = random.sample(range(1,50), 6)
boleto = sorted(loteria)
choice = input('Que lotería quieres jugar hoy? ')
if choice in options:
print('Tus numeros de', choice, 'son:', boleto, 'con reintegro', reintegro)
else:
print('Esa lotería no existe!')
Upvotes: 0
Views: 49
Reputation: 9704
You need to use a loop - A while loop is perfect here:
import random
options = ("Bonoloto", "Primitiva", "Euromillón", "Gordo")
reintegro = random.sample(range(1,9), 1)
loteria = random.sample(range(1,50), 6)
boleto = sorted(loteria)
while True:
choice = input('Que lotería quieres jugar hoy? ')
if choice in options:
print('Tus numeros de', choice, 'son:', boleto, 'con reintegro', reintegro)
break
else:
print('Esa lotería no existe!')
This code will continue to ask for a lottery name until they choose one in the list
Upvotes: 0
Reputation: 177610
Wrap it in a loop and break the loop when successful.
while True:
choice = input('Que lotería quieres jugar hoy? ')
if choice in options:
print('Tus numeros de', choice, 'son:', boleto, 'con reintegro', reintegro)
break
else:
print('Esa lotería no existe!')
Upvotes: 2