How to run table algorithm again if user chooses to run again?

Asked

Viewed 33 times

-1

from time import sleep
print('-=-'*20)
print('Tabuada da adição')
print('-=-'*20)
n = int(input('Insira um número: '))
n0 = n + 0 
n1 = n + 1
n2 = n + 2
n3 = n + 3
n4 = n + 4
n5 = n + 5
n6 = n + 6
n7 = n + 7
n8 = n + 8
n9 = n + 9
n10 = n + 10
sleep(2)
print('Aguarde um pouco enquanto estou carregando.....')
print('{}+0 = {}'.format(n, n0))
print('{}+1 = {}'.format(n, n1))
print('{}+2 = {}'.format(n, n2))
print('{}+3 = {}'.format(n, n3))
print('{}+4 = {}'.format(n, n4))
print('{}+5 = {}'.format(n, n5))
print('{}+6 = {}'.format(n, n6))
print('{}+7 = {}'.format(n, n7))
print('{}+8 = {}'.fomrat(n, n8))
print('{}+9 = {}'.format(n, n9))
print('{}+10 = {}'.format(n, n10))
r = str(input('Deseja escolher outro número ? '))

1 answer

2

One way to solve the problem would be to place your code within a loop loop loop structure and keep the execution of the program loop until the user makes the option to exit the program.

Here is an example of how it can be implemented:

repetir = True
while repetir:
    #seu código
    entrada = input('Deseja executar novamente? (s/n)')
    if entrada[0].lower() == 'n':
        repetir = False

For this example we use the loop while, the function lower class str and we are extracting the first position of the array, to make the decision to display again.

Notice that in this way the only way to stop the repetition is by responding "n", was implemented this way for simplicity, this implementation will not consider the answer "s", any user choice (except the answer n) will cause there to be a new repeat of the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.