想知道為什么在這種情況下它會兩次列印我的 else 陳述句。從理論上講,它應該在讀取不正確的密鑰后只執行一次,然后回圈回來。 按下非“輸入”鍵后我得到的輸出如何避免這種情況?
import random
import keyboard
#Constant values to assign as a win or loss
_WIN = 1
_LOSE = 0
#Create and store game hand choices
handChoice = ['Spock', 'Lizard', 'Rock', 'Paper', 'Scissors']
print('\nThis is advanced Rock, Paper, Scissors! Press ENTER to start the automated game->')
while True:
if keyboard.read_key() == 'enter':
break
else:
print('Please press ENTER!')
#Hand choices randomly assigned to player 1
player1 = random.choice(handChoice)
print('Player 1 uses ' player1 '!\n')
uj5u.com熱心網友回復:
看起來真實的檔案你可以使用這個:
keyboard.wait('enter')
它也將使用更少的cpu。但是您需要帶有超時的異步庫來提供輸出。你也可以使用這個:
while True:
if keyboard.read_key() == 'enter':
break
elif event.event_type == keyboard.KEY_DOWN:
print('Please press ENTER!')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420067.html
標籤:
下一篇:numpy將值帶入一個范圍
