似乎无法让 .getch() 工作 (Python 2.7)
Can't seem to get .getch() to work (Python 2.7)
我正在尝试检测按键以确定用户是否想再次玩游戏,但 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
我正在尝试检测按键以确定用户是否想再次玩游戏,但 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