我正在尝试检测按键是否确定用户是否要再次玩,但是msvcrt.getch()不适用于我。这是我的代码:
import msvcrt
#the game here
print "Do you want to continue? Y/N"
if msvcrt.getch() == 'Y' or msvcrt.getch() == 'y':
print"Let's play again!"
print"-----------------"
elif msvcrt.getch() == 'N' or msvcrt.getch() == 'n' :
"Okay, hope you had fun"
break
有什么建议么?
编辑:下面的答案确实可以在命令行上工作,由于某种原因只是不在PyCharm中
您只能打msvcrt.getch()
一次电话。将您的代码更改为如下所示:
import msvcrt
#the game here
print "Do you want to continue? Y/N"
response = msvcrt.getch()
if response.lower() == 'y':
print"Let's play again!"
print"-----------------"
elif response.lower == 'n' :
"Okay, hope you had fun"
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句