从硬编码打印是可行的,但是如何从Python中定义的方法打印这些变量输入的值呢?

2024-04-23 21:35:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Sublime Text 3,并在带有Python解释器3.6.3的Ubuntu16.04终端中运行代码。以下是我试图解决的部分代码:

    import sys
    print(sys.version_info)
    import math
    import cmath
    import datetime
    import random as RAN
    from tabulate import tabulate

    class Person:

        def __init__(self, firstName, lastName, birthdate, email, idx):
            self.firstName = firstName
            self.lastName = lastName
            self.birthdate = birthdate
            self.email = email
            self.idx = idx
            self._age = None
            self._age_last_recalc = None
            self._recalc_age()
        def _recalc_age(self):
            today = datetime.date.today()
            age = today.year - self.birthdate.year
            if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
                age -= 1
            self._age = age
            self._age_last_recalc = today
        def age(self):
            if (datetime.date.today() > self._age_last_recalc):
                self._recalc_age()
            return self._age

    def KeyInPosNumber(n, lower, upper):
        while True:
            try:
                n = int(input('Enter> '))
            except ValueError:
                print("Invalid input. Please try again.\n")
                continue
            if not n in range (lower, upper):
                print("Value cannot be under {0} or exceed {1}. Please try again.\n".format(lower, upper))
                continue
            else:
                break

    def KeyInString(n):
        while True:
            try:
                n = str(input('Enter> '))
            except StandardError:
                print("Error encountered! Please try again.\n")
                continue
            else:
                break

    def main():         
        girl = Person("Jane", "Doe", datetime.date(1992, 9, 15), "jane.doe@em.com", "53A")
        print("")
        print("Example student:") #Here's the hard-coded version
        print(girl.firstName)
        print(girl.lastName)
        print(girl.age())
        print(girl.email)
        print(girl.idx)
        print("")
        print("")

        print("Key in the student's birth year in the format YYYY.")
        BYear = 0; 
        BYear = KeyInPosNumber(BYear, 1900, 2015)
        print("\nKey in the student's birth month in the format MM.")
        BMonth = 0; 
        BMonth = KeyInPosNumber(BMonth, 1, 13)
        print("\nKey in the student's birth day number in the format DD.")
        BDay = 0; 
        BDay = KeyInPosNumber(BDay, 1, 32)
        print("\nKey in the student's first name.")
        FName = ""; 
        FName = KeyInString(FName)
        print("\nKey in the student's last name.")
        LName = ""; 
        LName = KeyInString(LName)
        print("\nKey in the student's email address (without '@' and domain.)")
        EMail = ""; 
        EMail = KeyInString(EMail)
        print("\nKey in the student's registration index.")
        RIndex = ""; 
        Rindex = KeyInString(RIndex)

        kid = Person(FName, LName, datetime.date(BYear, BMonth, BDay), EMail, RIndex) #Here's the input version
        print("")
        print(kid.firstName)
        print(kid.lastName)
        print(kid.age())
        print(kid.email)
        print(kid.idx)      

    if __name__ == '__main__':
        main()

我可以打印出硬编码的女孩,但不能打印出输入的孩子。我花了很多时间与我的培训师和互联网解决这个问题

线路端点错误:

    kid = Person(FName, LName, datetime.date(BYear, BMonth, BDay), EMail, RIndex)

错误说明:TypeError:需要整数(get type NoneType)

谢谢你的帮助


Tags: theinimportselfagetodaydatetimedate
1条回答
网友
1楼 · 发布于 2024-04-23 21:35:10

KeyInString(n)函数和另一个函数没有返回,因此变量中没有存储任何内容

试试这个:

def KeyInString(n):
    while True:
        try:
            n = str(input('Enter> '))
        except StandardError:
            print("Error encountered! Please try again.\n")
            continue
    return n

你也不需要给函数传递一个我推荐的参数:

def KeyInString():
    while True:
        try:
            n = str(input('Enter> '))
        except StandardError:
            print("Error encountered! Please try again.\n")
            continue
    return n

使用以下方法调用:

EMail = KeyInString()

但你永远是老板,想干什么就干什么:)

相关问题 更多 >