gpt4 book ai didi

python - 似乎无法让 .getch() 工作(Python 2.7)

转载 作者:行者123 更新时间:2023-12-05 07:40:20 27 4
gpt4 key购买 nike

我正在尝试检测按键以确定用户是否想再次玩游戏,但 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

关于python - 似乎无法让 .getch() 工作(Python 2.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46225236/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com