我正在尝试这样做,以便在插入我的名字时检查它是否有数字(成功)以及名字是否长。一旦我再次输入名称,它就会再次检查所有名称。它适用于检查数字,但不适用于检查数字是否太长。而是继续执行代码。
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
P.S:我将其从葡萄牙语翻译为英语,以便您可以更好地阅读,但我可能会犯一些错误。 nome()
答案 0 :(得分:2)
如果用户输入的数字名称,则代码进入if number == True:
块。然后,当用户输入其他名称时,您将计算number = hasNumbers(nome)
,但不会再次运行nameLongo
。因此long
仍在引用 previous 名称是否太长。您需要在两个分支中分别调用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
答案 1 :(得分:0)
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()
答案 2 :(得分:0)
如果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()