简体   繁体   中英

Where should I put continue for the code to go back to top of the loop?

So, I'm making a code where it asks for my name and then asks me if I'm sure that's my name. If I answer 'S' it continues, if I answer 'N' it asks me for my name again and it should go back to top of the loop and ask me if I'm sure, but it doesn't. If I don't answer neither 'S' nor 'N', it asks me again if I'm sure.

print('Hi! Whats your name?')
nome = input().title()
while len(nome) < 3 or len(nome) > 10:
    print('That name doesnt sound real. Please, tell me your real name.')
    nome = input().title()
else:
    respostass = ['Sim', 'S']
    respostasn = ['Não', 'N', 'Nao']
    print('Are you sure your names ' + nome + '?')
    certeza = input().title()
    while certeza not in respostass and certeza not in respostasn:
        print('I did not understood what you meant. Are you sure or not?')
        certeza = input().title()
    if certeza in respostass:
        print("Nice. Let us continue.")
    else:
        print('So, whats your name?')
        nome = input().title()
        continue

It gives me this error:

  File "C:/Users/Hugo/.PyCharmCE2019.1/config/scratches/scratch.py", line 22
    continue
    ^
SyntaxError: 'continue' not properly in loop

While formatting this I noticed you had some string issues. Please use " " and ' ' accordingly or you could have more syntax errors. But you don't really need a continue statement. I think you want to stop after the user confirms right?

print("Hi! What's your name?")
nome = input().title()
while len(nome) < 3 or len(nome) > 10:
    print("That name doesn't sound real. Please, tell me your real name.")
    nome = input().title()
else:
    respostass = ['Sim', 'S']
    respostasn = ['Não', 'N', 'Nao']
    print("Are you sure your name's " + nome + '?')
    certeza = input().title()
    while certeza not in respostass:
        if certeza not in respostasn:
            print('I did not understood what you meant. Are you sure or not?')
            certeza = input().title()
        else:
            print("So, what's your name?")
            nome = input().title()
            print("Are you sure your name's " + nome + '?')
            certeza = input().title()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM