Is there a way back to a particular line of code?

Asked

Viewed 69 times

-4

I would like to know if there is any module, function, etc. that would allow me to go back to a certain line of code.

I have this doubt because, whenever I want to ask a question repeatedly, I have to put the same variable again, as in lines 7 and 12

tupla = 'zero', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez', 'onze',\
    'doze', 'treze', 'quatorze', 'quinze', 'dezesseis', 'dezessete', 'dezoito', 'dezenove', 'vinte'
for num in tupla:
    num = int(input('Digite um número: '))
    ext = tupla[num]
    print(f'Você digitou o número {ext}')
    opcao = str(input('Quer continuar a ler os números por extenso? [S/N] ')).upper().strip()
    while opcao == 'S':
        num = int(input('Então digita outro número: '))
        ext = tupla[num]
        print(f'Você digitou o número {ext}')
        opcao = str(input('Quer continuar a ler os números por extenso? [S/N] ')).upper().strip()
    while opcao == 'N':
        exit('Obrigado por usar nosso programa.')

  • 1

    I don’t understand the question, you already use a loop of repetition while in your code. See Why in Python there is no drop?

  • The question I have is if there’s anything that makes me not need to replicate the question in the code.

  • So I’m going to rephrase. Why do you ask about repeating structures if you already use repeating structures in your code?

  • I think I have already understood why there is no way to refer to a certain line of code with the link you gave me about the fact that there is no goto in Python. However, just to be clear, what I wanted to know is if there was a way to not need to replicate, even if it is within a repeat structure, the same line of code.

1 answer

-1

If I understand your problem just use a python function for the operation you want to repeat. Function tutorial

Your code for example could be rewritten like this:

numeros_por_extenso = [
    'zero', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez', \
    'onze','doze', 'treze', 'quatorze', 'quinze', 'dezesseis', 'dezessete', 'dezoito', 'dezenove', 'vinte']

def retorna_numero_por_extenso():
    numero = int(input('Digite um número: '))
    numero_extenso = numeros_por_extenso[numero]
    print(f'Você digitou o número {numero_extenso}')

execucao = True

while execucao:
    retorna_numero_por_extenso()
    opcao = str(input('Quer continuar a ler os números por extenso? [S/N] ')).upper().strip()
    if opcao == 'N':
        execucao = False
        exit('Obrigado por usar nosso programa.')

Some details:

  • Try not to abbreviate the name of your variables
  • Do not use more than one while loop in the same code unless it is extremely necessary.

Browser other questions tagged

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