Cant define functions properly

Advertisements

The assignment goes as follows:
Create a program that will ask a user the year the were born and their name. The program should:

call a function that will ask the user for input
send that information back to the main function
call a function that calculates their age
call the function to print out their age, first initial, and last name.

I tried this

```
def main():
    first = input('Enter your first name: ')
    last = input('Enter your last name: ')
    year = int(input('What year were you born: '))
    again = 'y'
    
    def userInput(first, last, year):
        fname = first
        lname = last
        dob = year
        return fname, lname, dob
    def calcy(dob):  
    # age calculator
        age = 2023 - dob
        return age
    
    def printy(first, last, age):
    # Print Statement
        print(f'{fname[0]} {lname} is {age} years old.')
        #play again
        again = input('Play again? (y/n)')
        while again == 'y':
            first = input('Enter your first name: ')
            last = input('Enter your last name: ')
            year = int(input('Enter your year of birth: '))
            age = 2023 - year
            print(f'{first[0]} {last} is {age} years old.')
            playagain = input('Play again? (y/n)')
        print('Thank you for playing.')
main()
```

I cant seem to continue the loop, and it stops after the first inputs. What am I doing wrong? It needs 4 functions (main included)

>Solution :

Your code is almost correct! You defined function objects with def statements, but you did call only your main function object. So other function objects don’t do anything yet. You called main function with main() expression statement on the last line of your program, and to get it work we want to place function calls just in the body of our main function definition. So we have function definitions at our top-level of program, but we actually call defined objects within the main function definition, and we do that because it becomes logically structured and functionally easy to run the program with one main call, but the logic of our program is splitted into five different function objects, so it is easy to maintain the program and it further.

def userInput():
    first = input('Enter your first name: ')
    last = input('Enter your last name: ')
    dob = int(input('Enter year you were born: '))
    return first, last, dob

def calcy(dob):
    return 2023 - dob

def printy(first_name, last_name, persons_age):
    print(f'{first_name} {last_name} is {persons_age} years old.')

def main():
    
    first, last, dob = userInput()
    age = calcy(dob)
    printy(first, last, age)
    again = input('Play again? (y/n): ')
    while again != 'n':
        first, last, dob = userInput()
        age = calcy(dob)
        printy(first, last, age)
        again = input('Play again? (y/n): ')
        
main()

Also look at important changes that have been made in your code. We define all functions in our top-level of program, we don’t nest function definition inside another function definition. So this code is more neat and readable.

Leave a ReplyCancel reply