If you want something to happen over and over again, don’t make it up, use a loop. A very simple version would be:
while True:
# faz o que precisa aqui (lê os números, faz as contas, etc)
if input('Quer continuar? (S/N) ') == 'N':
break
The other answers use a variable to control the condition of the loop, but in this case I find it unnecessary. The value typed in the last input
will only be used there, to know whether to continue or not, and then will not be used for anything else. If you type N
, the break
interrupts the loop.
Of course you could do something like:
condicao = True
while condicao:
# faz o que precisa aqui (lê os números, faz as contas, etc)
if input('Quer continuar? (S/N) ') == 'N':
condicao = False
But I find it simpler to do the break
, which makes it clear that at that point the loop must be stopped.
Another detail is that if you type anything other than "N", the while
continues. If you want to be more specific and only accept certain values, you can do this separately:
while True:
# faz o que precisa aqui (lê os números, faz as contas, etc)
while True: # outro loop só para validar a opção
opcao = input('Quer continuar? (S/N) ')
if opcao not in ('S', 'N'):
print('Digite apenas S ou N')
else:
break # sai do while interno
if opcao == 'N':
break # sai do while externo
That is, another loop just to keep asking to type again if it’s not one of the valid options. Note that it now makes sense to have a variable to store the option, as I will use it more than once (to test if it is one of the valid values, and then to see if it is the option that terminates the program).
Of course, there you can sophisticate as much as you want/need. If you search around, you will see that "a lot of people" call upper()
to capitalize the string (so you could type "n" or "N", for example), or input(...)[0]
to pick only the first character (this has a flaw, which is in case the user type only ENTER, then input
returns the empty string and when trying to access the first character gives error).
If you want, separate the option reading into a specific function:
def ler_opcao(mensagem, valores_validos = ['S', 'N']):
validos = "/".join(valores_validos)
while True:
opcao = input(f'{mensagem} ({validos}) ')
if opcao not in valores_validos:
print(f'Digite apenas um dos valores válidos: {validos}')
else:
return opcao
while True:
# faz o que precisa aqui (lê os números, faz as contas, etc)
if ler_opcao('Quer continuar?') == 'N':
break
Thus, the function ler_opcao
receives the message and the list of valid values (and the message will already show these values based on the list). This way you can customize at will. For example, if you want the options to be 1, 2 or 3:
while True:
# faz o que precisa aqui (lê os números, faz as contas, etc)
opcao = ler_opcao('Digite 1 para fazer X, 2 para fazer Y, 3 para sair', ['1', '2', '3'])
if opcao == '1':
print('fazer X')
elif opcao == '2':
print('fazer Y')
elif opcao == '3':
break
Notice that in that case I don’t need to use int
to turn the option into a number, because if a number is not entered, it will not be among the valid options. And in this case it also makes sense to have a variable because it’s used several times later to test which of the values was typed.
In your specific case, then it would be:
while True:
# faz o que precisa aqui (lê os números, faz as contas, etc)
if ler_opcao('[ 1 ] Realizar outra conta\n[ 2 ] Sair do Programa', ['1', '2']) == '2':
break
That doesn’t make sense, make a
while
https://answall.com/q/352398/10 or https://answall.com/q/385771/101.– Maniero
I thought it was funny the name of your variable.
– Danizavtz
@Danizavtz will CU = Choose User, or Command User?
– Guilherme Nascimento