fn = input("Hello, what is your first name?")
firstname = (fn[0].upper())
ln = input("Hello, what is your last name?")
lastname = (ln.lower())
我希望fn在一个循环中,这样如果他们输入一个数字而不是字母,它会重复这个问题
答案 0 :(得分:3)
我想你需要这样的东西
final_fn = ""
while True:
fn = input("Hello, what is your first name?")
if valid(fn):
final_fn = fn
break
在它之前定义验证方法。一个例子就是Joran提到的
def valid(fn):
return fn.isalpha()
答案 1 :(得分:1)
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):