似乎无法使.getch()工作(Python2.7)

2024-06-25 07:12:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检测一个按键,以确定用户是否想再次播放,但是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中不起作用


Tags: orthe答案代码用户importyougame
1条回答
网友
1楼 · 发布于 2024-06-25 07:12:28

您应该只调用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

相关问题 更多 >