繁体   English   中英

我将如何使用while循环,以便如果他们输入数字,它将再次询问他们?

[英]How would I use a while loop so that if they enter a number it would ask them the again?

fn = input("Hello, what is your first name?")
firstname = (fn[0].upper())
ln = input("Hello, what is your last name?")
lastname = (ln.lower())

我希望fn处于循环状态,以便如果他们输入数字而不是字母,它将重复这个问题

我想你需要这样的东西

final_fn = ""
while True:
    fn = input("Hello, what is your first name?")
    if valid(fn):
        final_fn = fn
        break

在此之前定义您的验证方法。 一个例子就是乔兰提到的

def valid(fn):
    return fn.isalpha()
if result.isalpha():
   print "the string entered contains only letters !"

我猜 ?

a="6"
while not a.isalpha():
    a = raw_input("Enter your name:")
print "You entered:",a

如果您只想消除包含数字的单词,则可以这样做

while any(ltr.isdigit() for ltr in a):

暂无
暂无

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

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