The function input
returns a string, and we if
's you are comparing with numbers. So you can choose whether to compare with strings (by placing the numbers between quotes):
x = input('Select an option... ')
if x == '1':
print('xxxx')
elif x == '2':
print('yyyy')
elif x == '3':
print('zzzz')
else:
print('lalalalala')
Or becomes the result of input
in a number, using int
:
x = int(input('Select an option... '))
if x == 1:
print('xxxx')
elif x == 2:
print('yyyy')
elif x == 3:
print('zzzz')
else:
print('lalalalala')
Remembering that int
can launch a ValueError
if you don’t enter a number. Then you can use a while
which, until a number is entered, keeps asking the user to type again:
while True:
try:
x = int(input('Select an option... '))
break # número digitado, pode sair do while
except ValueError:
print('Você não digitou um número, tente novamente')
if x == 1:
print('xxxx')
elif x == 2:
print('yyyy')
elif x == 3:
print('zzzz')
else:
print('lalalalala')
Welcome to [en.so]! I removed the part in English because this site is only in Portuguese. And using the code that is in the question, there is no syntax error, so there must be some other detail elsewhere in the code...
– hkotsubo