在Python3中使用while循环获取工作。
在Python3中使用while循环获取工作。
这个问题在这里已经有了答案:
这个脚本可以在Python 2中工作,但在Python 3中不行,即使我输入了正确的答案,输入问题仍然会显示:
correct = "no" while correct == "no": answer = input("15 x 17? ") if answer == 15*17: correct = "yes" print 'good!' #for python2 else: correct = "no"
如何解决这个问题,既不使用函数也使用函数?
admin 更改状态以发布 2023年5月23日
为了在 Python 2.7 和 Python 3 中都能够运行,两个语言版本之间有几个不同之处-请参见Python 3.0 的新功能:
input()
在py3中相当于py2中的raw_input()
print
在py3中不再是一个语句而是一个函数
为了使此代码在py2.7和py3中都起作用,可以这样做:
from __future__ import print_function try: input = raw_input except NameError: pass while True: answer = int(input("15 x 17? ")) if answer == 15*17: print('good!') break