[英]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())
I want fn to be on a loop so that if they enter their a number instead of letters, it would repeat the question 我希望fn处于循环状态,以便如果他们输入数字而不是字母,它将重复这个问题
I guess you need something like this 我想你需要这样的东西
final_fn = ""
while True:
fn = input("Hello, what is your first name?")
if valid(fn):
final_fn = fn
break
Define you validation method before it. 在此之前定义您的验证方法。 An example would be as Joran mentioned 一个例子就是乔兰提到的
def valid(fn):
return fn.isalpha()
if result.isalpha():
print "the string entered contains only letters !"
I guess ? 我猜 ?
a="6"
while not a.isalpha():
a = raw_input("Enter your name:")
print "You entered:",a
if you just wanted to eliminate only words that contained numbers you could do 如果您只想消除包含数字的单词,则可以这样做
while any(ltr.isdigit() for ltr in a):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.