【发布时间】:2018-01-20 16:46:31
【问题描述】:
我为 Python 3 开发了这段代码,以询问用户是否希望继续:
def dnv():
b = input('Would you like to try again? Type 1 for "yes" and 0 for "no": ')
if b == '1':
return run()
if b == '0':
return print('See you later!')
run() 是任何程序的主函数。但是现在我正在学习 Python 2 的课程,并且我尝试将它改编为我从那里学习的算法,但没有成功。目的是确定两个日期之间有多少天。我相信相关部分是:
def inputs():
year1 = input('Type the first year: ')
month1 = input('Type the first month: ')
day1 = input('Type the first day: ')
year2 = input('Type the second year: ')
month2 = input('Type the second month: ')
day2 = input('Type the second day: ')
print daysBetweenDates(year1, month1, day1, year2, month2, day2)
def dnv():
answer = input('Would you like to try again? Type "y" for yes and "n" for no: ')
if answer == 'y':
return inputs()
else:
print('See you later!')
虽然每次我输入 y 或 n 时,我都会得到一个 NameError: name 'y' is not defined(类似于“n”)。我还尝试将if answer == 'y' 修改为:
if answer.lower().startswith('y'):
return inputs()
再次没有成功。我错过了什么?谢谢!
【问题讨论】:
-
python2
input()与 python3input()的工作方式不同。请改用raw_input()。 -
成功了!非常感谢。