2 回答
TA贡献1796条经验 获得超7个赞
def getName():
name = input("What is your name?: ")
return name
input()在 python2.7 中尝试评估它作为函数获得的任何内容-raw_input()改为使用获取字符串,该字符串将作为纯字符串返回。
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").
如果您未到退休年龄,getAge()将在第一个if语句中调用一次,然后在else语句中再次调用。考虑将结果分配给getAge()变量并重用它。此外,您在第二个语句.的末尾有一个迷路。print()
你是对的,你不需要使用datetime.
正确的代码如下:
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()
TA贡献2011条经验 获得超2个赞
将答案保存在变量中:
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").
添加回答
举报