我是否需要寻找一个键向下和向上的事件,以避免我的其他块重复出现



想知道为什么在这种情况下要打印两次我的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')

查看您可以使用的文档:

keyboard.wait('enter')

它还将使用更少的cpu。但是您需要带超时的异步库来提供输出。你也可以使用这个:

while True:
if keyboard.read_key() == 'enter':
break
elif event.event_type == keyboard.KEY_DOWN:
print('Please press ENTER!')

最新更新