简体   繁体   English

while循环不会回到顶部

[英]The while loop isn't going back to the top

I'm trying to do so that when I insert my name it checks if it has numbers in it (success) and if the name is to long. 我正在尝试这样做,以便在插入我的名字时检查它是否有数字(成功)以及名字是否长。 And once I type the name again it checks it all again. 一旦我再次输入名称,它就会再次检查所有名称。 it works with checking the numbers, but it doesn't work with checking if it's to long. 它适用于检查数字,但不适用于检查数字是否太长。 Instead, it just continues the code. 相反,它只是继续执行代码。

print('Hi! Whats your name?')
def nome():
    global pontos
    def hasNumbers(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'has numbers')
    def longName(longevidade):
        return len(nome) < 3 or len(nome) > 13
    nome = input().title()
    number = hasNumbers(nome)
    long = longName(nome)
    while number == True or long == True:
        if number == True:
            print('A name cant have any numbers. Please tell me your real name')
            nome = input().title()
            number = hasNumbers(nome)
            continue
        elif long == True:
            print('Your name is too long to be real. Please tell me your real name.')
            print(longName(nome))
            nome = input().title()
            long = longName(nome)
            continue

PS: I translated it from portuguese to english so you can read it better, but I might have made some mistakes. PS:我将其从葡萄牙语翻译为英语,以便您可以更好地阅读,但我可能会犯一些错误。 nome() nome()

If the user enters a name with digits, the code enters the if number == True: block. 如果用户输入的数字名称,则代码将输入if number == True:块。 Then when the user enters another name, you calculate number = hasNumbers(nome) , but you don't run nameLongo again. 然后,当用户输入另一个名称时,您将计算number = hasNumbers(nome) ,但不会再次运行nameLongo So long is still referencing whether the previous name is too long. 这么long仍在引用以前的名称是否太长。 You need to call nameLongo in both branches, and hasNumbers in both branches. 您需要在两个分支中都调用nameLongo在两个分支中都具有hasNumbers

print('Name:')
def nome():
    global pontos
    def temNumeros(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'tem numeros')
    def nomeLongo(longevidade):
        return len(nome) < 3 or len(nome) > 13
    nome = input().title()
    number = temNumeros(nome)
    long = nomeLongo(nome)
    while number == True or long == True:
        if number == True:
            print('digits.')
            nome = input().title()
            number = temNumeros(nome)
            long = nomeLongo(nome)
            continue
        elif long == True:
            print('Too long.')
            print(nomeLongo(nome))
            nome = input().title()
            number = temNumeros(nome)
            long = nomeLongo(nome)
            continue

nome()

Result: 结果:

Name:
123
digits.
ffffffffffffffffffffffffffffffffffffffffffffff
Too long.
True
kevin

... But ultimately I think it would be easier to call input exactly once in the loop, rather than in each branch of the conditional. ...但是最终,我认为在循环中准确地一次调用input要比在条件语句的每个分支中调用更容易。

def contains_digits(name):
    return any(char.isdigit() for char in name)

def is_wrong_length(name):
    return len(name) < 3 or len(name) > 13

while True:
    name = input("Name: ")
    if contains_digits(name):
        print("Name can't contain digits.")
    elif is_wrong_length(name):
        print("Name must be between 3 and 13 characters.")
    else:
        #name is valid, so exit the loop
        break

print("Welcome,", name)

Result: 结果:

Name: 123
Name can't contain digits.
Name: ffffffffffffffffffffffffffffffffffffffffff
Name must be between 3 and 13 characters.
Name: Kevin
Welcome, Kevin
def longName(longevidade):
    return len(longevidade) < 3 or len(longevidade) > 13

you were not putting 'longevidade' as the arguments of len() 您没有将“ longevidade”作为len()的参数

EDIT : Here is your problem you were also not updating the value of number and long 编辑:这是您的问题,您也没有更新number和long的值

print('Hi! Whats your name?')
def nome():
    global pontos
    def hasNumbers(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'has numbers')
    def longName(longevidade):
        return len(longevidade) < 3 or len(longevidade) > 13
    nome = input().title()
    number = hasNumbers(nome)
    long = longName(nome)
    while number == True or long == True:
        if number == True:
            print('A name cant have any numbers. Please tell me your real name')
            nome = input().title()
            number = hasNumbers(nome)
            long = longName(nome)
            continue
        elif long == True:
            print('Your name is too long to be real. Please tell me your real name.')
            print(longName(nome))
            nome = input().title()
            long = longName(nome)
            number = hasNumbers(nome)
            continue
nome()

If a while loop didn't play again that means the statement you given for it isn't correct 如果while循环没有再次播放,则意味着您给出的语句不正确

Your statement is 您的声明是

while number == True or long == True:

so it only ran once because the second run after the loop all of your statements 所以它只运行一次,因为第二次运行是在循环所有语句之后

number == True 

and this statement 这句话

long == True

is not working . 不管用 。 What I suggest is you run each statement separately and see which one is working this way you can solve the issue 我的建议是,您分别运行每个语句,然后看看哪种方法可以解决该问题

or create function for each like 或为每个喜欢的人创建功能

def number():
    if number == True:
        print('A name cant have any numbers. Please tell me your real name')
        nome = input().title()
        number = hasNumbers(nome)
    else:
       pass

def long():
    if long == True:
        print('Your name is too long to be real. Please tell me your real name.')
        print(longName(nome))
        nome = input().title()
        long = longName(nome)
    else:
        pass

while True:
    number()
    long()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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