我是否需要查找按键按下和按下事件以避免重复出现我的 else 块?
Do I need to look for a key down and up event to avoid double occurrence of my else block?
想知道为什么在这种情况下它会打印我的 else 语句两次。从理论上讲,它应该在读取到不正确的密钥后只执行一次,然后循环返回。
the out put I am getting after pressing a non 'enter' key 我怎样才能避免这种情况?
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')
看起来不错the docs你可以用这个:
keyboard.wait('enter')
也会少用cpu。但是您需要带有超时的异步库来提供输出。
你也可以使用这个:
while True:
if keyboard.read_key() == 'enter':
break
elif event.event_type == keyboard.KEY_DOWN:
print('Please press ENTER!')
想知道为什么在这种情况下它会打印我的 else 语句两次。从理论上讲,它应该在读取到不正确的密钥后只执行一次,然后循环返回。 the out put I am getting after pressing a non 'enter' key 我怎样才能避免这种情况?
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')
看起来不错the docs你可以用这个:
keyboard.wait('enter')
也会少用cpu。但是您需要带有超时的异步库来提供输出。 你也可以使用这个:
while True:
if keyboard.read_key() == 'enter':
break
elif event.event_type == keyboard.KEY_DOWN:
print('Please press ENTER!')