-1

I am trying to calculate the age of a person, to verify whether or not he is of retirement age. This is python 2.7

from datetime import datetime

def getName():
  name = input("What is your name?: ")
  return name

def getAge():
  age = input("How old are you?: ")
  return int(age)

def ifOrNot():
  retirementAge = 65
  if(getAge() >= retirementAge):
    print("Hello  ",getName(), ", are you in retirment age")
  else:
    timeToRetirement = retirementAge - getAge()
    print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more").

def main():
  ifOrNot()

main()

The question "how old are you" appears on the screen twice. Then the question "What's your name" is displayed once. And then these errors:

Traceback (most recent call last):   File "main.py", line 22, in
<module>
    main()   File "main.py", line 20, in main
    ifOrNot()   File "main.py", line 17, in ifOrNot
    print("Hola ", getName(), " aun no estas en edad de retiro, te faltan ", timeToRetirement)   File "main.py", line 4, in getName
    name = input("What is your name?: ")   File "<string>", line 1, in <module> NameError: name 'Diesan' is not defined

I want to ask for the name first, then the age, and calculate the remaining years to be able to retirement. Curiosly, I do not need to use datetime, right?

martineau
  • 119,623
  • 25
  • 170
  • 301
Diesan Romero
  • 1,292
  • 4
  • 20
  • 45

2 Answers2

3
def getName():
  name = input("What is your name?: ")
  return name

input() in python2.7 tries to evaluate whatever it gets as a function - use raw_input() instead to get the string, which will be returned as a plain string.

def ifOrNot():
  retirementAge = 65
  if(getAge() >= retirementAge):
    print("Hello  ",getName(), ", are you in retirment age")
  else:
    timeToRetirement = retirementAge - getAge()
    print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more").

If you're not of retirement age, getAge() will be called once in the first if statement, and then again inside the else statement. Consider assigning the result got getAge() to a variable and reusing it. Also, you've got a stray . at the end of your second print() statement.

You are correct, you do not need to use datetime.

Correct code is as follows:

def getName():
  name = raw_input("What is your name?: ")
  return name

def getAge():
  age = input("How old are you?: ")
  return int(age)

def ifOrNot():
  retirementAge = 65
  myAge = getAge()
  if(myAge >= retirementAge):
    print("Hello  ",getName(), ", are you in retirment age")
  else:
    timeToRetirement = retirementAge - myAge
    print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more")

def main():
  ifOrNot()

main()

Demo

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
1

Keep the answers in variables:

def ifOrNot():
    retirementAge = 65
    name = getName()
    age = getAge()
    if (age >= retirementAge):
        print("Hello  ", name, ", are you in retirment age")
    else:
        timeToRetirement = retirementAge - age
        print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more").
ipaleka
  • 3,745
  • 2
  • 13
  • 33