1
我試圖檢測一個按鍵來確定用戶是否想再次播放,但msvcrt.getch()只是不適合我。這是我的代碼:似乎無法得到.getch()工作(Python 2.7)
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
運行代碼時是否有任何錯誤? – cosinepenguin
您需要調用'getch()'__once__,然後比較該值四次。事實上,你只是比較用戶的初始按鍵和Y按鍵,然後請求第二個按鍵。 – jasonharper
@cosinepenguin @jasonharper沒有錯誤,只是沒有註冊任何按鍵,就好像'getch()'doesn出於某種原因甚至不會被召喚。 – sOfekS