Stoverflow
Stoverflow

Reputation: 65

Printing from hard-coding works but how do I print the values from these variable inputs from defined methods in Python?

I'm using Sublime Text 3 and running the code in Ubuntu 16.04 Terminal with Python interpreter 3.6.3. Here's part of my code I'm trying to resolve:

    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), "[email protected]", "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()

I can print out the hard-coded girl but not the input kid. Spent many hours trying to resolve this with my trainer and the internet.

Error in Terminal points to the line:

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

Error says: TypeError: an integer is required (got type NoneType)

Thank you for your help.

Upvotes: 0

Views: 271

Answers (1)

BigZee
BigZee

Reputation: 496

There is no return for the KeyInString(n) function and the other function, therefore, nothing is stored in the variable.

try this:

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

and also you don't need to necessarily pass the function a parameter I would recommend:

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

call it using:

EMail = KeyInString()

but as always you are the boss so do as you like :)

Upvotes: 1

Related Questions