我在尝试计算退休年龄的脚本中有错误

我正在尝试计算一个人的年龄,以验证他是否已达到退休年龄。这是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()

“你多大了”这个问题在屏幕上出现了两次。然后显示一次“你叫什么名字”这个问题。然后是这些错误:


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

我想先问名字,再问年龄,然后计算出能够退休的剩余年限。奇怪的是,我不需要使用datetime,对吧?


不负相思意
浏览 85回答 2
2回答

芜湖不芜

def getName():&nbsp; name = input("What is your name?: ")&nbsp; return nameinput()在 python2.7 中尝试评估它作为函数获得的任何内容-raw_input()改为使用获取字符串,该字符串将作为纯字符串返回。def ifOrNot():&nbsp; retirementAge = 65&nbsp; if(getAge() >= retirementAge):&nbsp; &nbsp; print("Hello&nbsp; ",getName(), ", are you in retirment age")&nbsp; else:&nbsp; &nbsp; timeToRetirement = retirementAge - getAge()&nbsp; &nbsp; print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more").如果您未到退休年龄,getAge()将在第一个if语句中调用一次,然后在else语句中再次调用。考虑将结果分配给getAge()变量并重用它。此外,您在第二个语句.的末尾有一个迷路。print()你是对的,你不需要使用datetime.正确的代码如下:def getName():&nbsp; name = raw_input("What is your name?: ")&nbsp; return namedef getAge():&nbsp; age = input("How old are you?: ")&nbsp; return int(age)def ifOrNot():&nbsp; retirementAge = 65&nbsp; myAge = getAge()&nbsp; if(myAge >= retirementAge):&nbsp; &nbsp; print("Hello&nbsp; ",getName(), ", are you in retirment age")&nbsp; else:&nbsp; &nbsp; timeToRetirement = retirementAge - myAge&nbsp; &nbsp; print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more")def main():&nbsp; ifOrNot()main()

森林海

将答案保存在变量中:def ifOrNot():&nbsp; &nbsp; retirementAge = 65&nbsp; &nbsp; name = getName()&nbsp; &nbsp; age = getAge()&nbsp; &nbsp; if (age >= retirementAge):&nbsp; &nbsp; &nbsp; &nbsp; print("Hello&nbsp; ", name, ", are you in retirment age")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; timeToRetirement = retirementAge - age&nbsp; &nbsp; &nbsp; &nbsp; print("Hello ", getName(), " are you not in retirement age, you need to wait ", timeToRetirement, " more").
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python