简体   繁体   中英

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

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):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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