Cant define functions properly [closed]

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)

  • Where do you assign again to another value?

    – 

  • 4

    You are defining three functions inside main which you never actually call.

    – 

  • Really, a couple of errors here. You need to call your functions somewhere. As of now, your loop is never called. Plus, playagain is not the same variable as again.

    – 

  • I fixed my little playagain error, what am i doing wrong with the functions?

    – 

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 other function calls straight into 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 the program becomes more structured and although it is easy to run the program with just one main call, we have five different self-contained and self-sufficient function objects – so it is easy to maintain the program and expand 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()

Leave a Comment