繁体   English   中英

while循环不会回到顶部

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

我正在尝试这样做,以便在插入我的名字时检查它是否有数字(成功)以及名字是否长。 一旦我再次输入名称,它就会再次检查所有名称。 它适用于检查数字,但不适用于检查数字是否太长。 相反,它只是继续执行代码。

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:我将其从葡萄牙语翻译为英语,以便您可以更好地阅读,但我可能会犯一些错误。 nome()

如果用户输入的数字名称,则代码将输入if number == True:块。 然后,当用户输入另一个名称时,您将计算number = hasNumbers(nome) ,但不会再次运行nameLongo 这么long仍在引用以前的名称是否太长。 您需要在两个分支中都调用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()

结果:

Name:
123
digits.
ffffffffffffffffffffffffffffffffffffffffffffff
Too long.
True
kevin

...但是最终,我认为在循环中准确地一次调用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)

结果:

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

您没有将“ longevidade”作为len()的参数

编辑:这是您的问题,您也没有更新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()

如果while循环没有再次播放,则意味着您给出的语句不正确

您的声明是

while number == True or long == True:

所以它只运行一次,因为第二次运行是在循环所有语句之后

number == True 

这句话

long == True

不管用 。 我的建议是,您分别运行每个语句,然后看看哪种方法可以解决该问题

或为每个喜欢的人创建功能

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