检测到键后,多次执行一个循环
我有代码:
while True:
if keyboard.is_pressed("i"):
print(usr)
因此,当我运行程序的其余部分并进入代码时,代码可行!它检测到我在键盘上按“我”。但是,它多次打印字典usr
。无论如何我可以解决这个问题吗?我尝试了break
关键字,但随后它停止了整个程序。请帮我!对不起,如果这不是最好的问题?我不太确定我关心整个声誉的事情,但是,我想确保自己撰写正确的帖子。
整个代码:
# My very own first game
# Completely made by Cyb3rst0rm
import keyboard
print('Hello, welcome to Terminal Adventure!')
q0 = input("What is your name? ") # q0, name
print('Nice to meet you ' + q0 + '!')
questions = ["Do you like fruit? Yes or No ",
"Do you like vegetables? Yes or No ",
"Do you play any sports? Yes or No ",
"Do you like chocolate? Yes or No ",
"What is your age? ", # index 4, question 5, q5, age
"What is your race? ", # index 5, question 6, q6, race
"What is your gender? ", # index 6, question 7, q7, gender
]
user = {}
def infoCall(usr):
while True:
if keyboard.is_pressed("i"):
print(usr)
def mainLoop():
q1 = input(questions[0])
if q1 == 'Yes':
q1 = input("What fruit do you like? ")
print("Cool! I can't eat " + q1 + " because I'm not a human.")
elif q1 == 'No':
print("Haha, you're not alone in that! ")
q2 = input(questions[1])
if q2 == 'No':
print("Me neither! They're disgusting!")
elif q2 == 'Yes':
print("Weirdo, haha")
q2 = input("What vegetable do you like? ")
print("I'm not so sure about that one.")
q3 = input(questions[2])
if q3 == 'Yes':
q3 = input("What sport? ")
print(q3 + " sounds fun!")
if q3 == 'No':
print("Hey we're both couch-potatoes!")
q4 = input(questions[3])
if q4 == "No":
q4 = input("Chocolate's amazing! Why don't you like it? ")
print("I don't agree with that, I'm sorry, chocolate is amazing.")
elif q4 == "Yes":
q4 = input("Hey me too! What type of chocolate do you like? ")
print("Cool! I like dark chocolate.")
q5 = input(questions[4])
q6 = input(questions[5])
q7 = input(questions[6])
user = {"name": q0, "age": q5, "race": q6, "gender": q7}
print("Here is the information you entered!")
print(user)
infoCall(user)
def askBegin():
begin = input("Shall we begin the game? Yes or No ")
if begin == 'Yes':
mainLoop()
elif begin == 'No':
quit
else:
print('Not an accepted answer! Please try again.')
askBegin()
askBegin()
I have the code:
while True:
if keyboard.is_pressed("i"):
print(usr)
So when I run the rest of the program and get to the code, the code works! It detects me pressing 'i' on my keyboard. However, it prints the dictionary usr
multiple times. Anyway I can fix this? I tried the break
keyword but then it stops the whole program. Please help me! I'm sorry if this isn't the best question? I'm not too sure I care about the whole reputation thing, however, I want to make sure I'm making the right posts.
The whole code:
# My very own first game
# Completely made by Cyb3rst0rm
import keyboard
print('Hello, welcome to Terminal Adventure!')
q0 = input("What is your name? ") # q0, name
print('Nice to meet you ' + q0 + '!')
questions = ["Do you like fruit? Yes or No ",
"Do you like vegetables? Yes or No ",
"Do you play any sports? Yes or No ",
"Do you like chocolate? Yes or No ",
"What is your age? ", # index 4, question 5, q5, age
"What is your race? ", # index 5, question 6, q6, race
"What is your gender? ", # index 6, question 7, q7, gender
]
user = {}
def infoCall(usr):
while True:
if keyboard.is_pressed("i"):
print(usr)
def mainLoop():
q1 = input(questions[0])
if q1 == 'Yes':
q1 = input("What fruit do you like? ")
print("Cool! I can't eat " + q1 + " because I'm not a human.")
elif q1 == 'No':
print("Haha, you're not alone in that! ")
q2 = input(questions[1])
if q2 == 'No':
print("Me neither! They're disgusting!")
elif q2 == 'Yes':
print("Weirdo, haha")
q2 = input("What vegetable do you like? ")
print("I'm not so sure about that one.")
q3 = input(questions[2])
if q3 == 'Yes':
q3 = input("What sport? ")
print(q3 + " sounds fun!")
if q3 == 'No':
print("Hey we're both couch-potatoes!")
q4 = input(questions[3])
if q4 == "No":
q4 = input("Chocolate's amazing! Why don't you like it? ")
print("I don't agree with that, I'm sorry, chocolate is amazing.")
elif q4 == "Yes":
q4 = input("Hey me too! What type of chocolate do you like? ")
print("Cool! I like dark chocolate.")
q5 = input(questions[4])
q6 = input(questions[5])
q7 = input(questions[6])
user = {"name": q0, "age": q5, "race": q6, "gender": q7}
print("Here is the information you entered!")
print(user)
infoCall(user)
def askBegin():
begin = input("Shall we begin the game? Yes or No ")
if begin == 'Yes':
mainLoop()
elif begin == 'No':
quit
else:
print('Not an accepted answer! Please try again.')
askBegin()
askBegin()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解,则应在与
print()
的相同级别上添加break
。这种行为的原因可能是print
比is_pressed
快得多。如果您希望它永远继续下去,则仅在按下“我”时打印用户,请尝试以下操作:
If I understood correctly, you should add
break
at the same level asprint()
. The cause of this behavior might be thatprint
is much faster thanis_pressed
.If you want it continue forever, only print user if "i" is pressed, try this: