如何检查用户';的输入是有效的Python

如何检查用户';的输入是有效的Python,python,Python,这是我的练习:获取用户的名字、姓氏和出生年份。根据姓名(第一个字母)创建首字母,并计算用户的年龄 我写了一个剧本: def age_name(): # var= firstName ,lastName ,year is_running=True firstName =input("What is your name?").lower() while is_running==True: if firstName.isalpha() and len(f

这是我的练习:获取用户的名字、姓氏和出生年份。根据姓名(第一个字母)创建首字母,并计算用户的年龄

我写了一个剧本:

def age_name():
    # var= firstName ,lastName ,year
    is_running=True
    firstName =input("What is your name?").lower()
    while is_running==True:
        if firstName.isalpha() and len(firstName)>0:
            lastName = input("What is your last name?").lower()
            if lastName.isalpha() and len(lastName) > 0:
                year = input("What is your birth year?")
                if year.isnumeric() or len(year)==4:
                    year=int(year)
                    print("Your initials are {0}{1} and your age is {2}.".format(firstName[0],lastName[0],2018-year))
                    #print("Your initials are ",firstName[0],lastName[0],"and your age is ",str(2018-year))
                else:
                    print("invalid year. please type your birth year")
                    year = input("What is your birth year?")
            else:
                print("invalid last name. please type your last name")
                lastName = input("What is you last name?").lower()
        else:
            print("invalid name. please type your name")
            firstName = input("What is you name?").lower()

age_name()
我已经测试了代码,结果如下:

What is your name?p5
invalid name. please type your name
What is you name?peter
What is your last name?p5
invalid last name. please type your last name
What is you last name?pen
What is your last name?pen
What is your birth year?p5
invalid year. please type your birth year
What is your birth year?10
What is your last name?pen
What is your birth year?1800
Your initials are pp and your age is 218.
What is your last name?
首先,我认为我的代码有点重复——如果有人想缩短它的话。第二个也是最大的问题——我一直在问姓氏问题。我的bug在哪里?

这看起来怎么样

def age_name():
    firstname=input("What is your name?")
    while firstname.isalpha()!=True:
        print("Invalid name, enter again")
        firstname = input("What is your name?")
    lastname = input("What is your last name?")
    while lastname.isalpha()!=True:
        print("Invalid name, enter again")
        lastname = input("What is your last name?")
    year = input("What is your birth year?")
    while len(year) != 4 and year.isdigit():
        print("Invalid year, enter again")
        year = input("What is your birth year?")
    year = int(year)
    print("First Name:",firstname,"\nLast Name:",lastname,"\nBirth Year:",year)

age_name()

我认为最好的可读性,而最不容易出错的方法是下面这样的。每个输入行只编程一次,所有内容都取决于
响应\u invalid
的状态

def age_name():
    response_invalid = True
    while response_invalid:
        firstName = input("What is your name?").lower()
        if firstName.isalpha() and len(firstName)>0:
            response_invalid = False
        else:
            print("invalid name. please type your name")
    response_invalid = True
    while response_invalid:
        lastName = input("What is your last name?").lower()
        if lastName.isalpha() and len(lastName) > 0:
            response_invalid = False
        else:
            print("invalid last name. please type your last name")
    response_invalid = True
    while response_invalid:
        year = input("What is your birth year?")
        if year.isnumeric() or len(year)==4:
            response_invalid = False
        else:
            print("invalid year. please type your birth year")
    year=int(year)
    print("Your initials are {0}{1} and your age is {2}.".format(firstName[0],lastName[0],2018-year))
然而,这是一种非常常见的实现方法的详细变体,我认为这是最短的,但有点粗鲁…:

def age_name():
    while True:
        firstName = input("What is your name?").lower()
        if firstName.isalpha() and len(firstName)>0:
            break
        print("invalid name. please type your name")
    while True:
        lastName = input("What is your last name?").lower()
        if lastName.isalpha() and len(lastName) > 0:
            break
        print("invalid last name. please type your last name")
    while True:
        year = input("What is your birth year?")
        if year.isnumeric() or len(year)==4:
            break
        print("invalid year. please type your birth year")
    year=int(year)
    print("Your initials are {0}{1} and your age is {2}.".format(firstName[0],lastName[0],2018-year))

看起来好多了。非常感谢。 但请看我在输入year whis字母时出现的错误: 你的出生年份是什么?g5
ValueError:以10为基数的int()的文本无效:“g5”。
我已将上一个while操作符从和更改为或,它工作得很好。

看起来您从未更改
是否正在运行
?欢迎使用StackOverflow。我会考虑把三个部分中的每一个(名字,姓氏,年份)放在一个单独的函数中。目前,一个无效的条目扰乱了流程。一旦完成,您应该在某个时候设置
isRunning=False
。创建特定函数和错误处理也是