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()
CodePudding user response:
If I understood correctly, you should add break
at the same level as print()
. The cause of this behavior might be that print
is much faster than is_pressed
.
def infoCall(usr):
while True:
if keyboard.is_pressed("i"):
print(usr)
break
If you want it continue forever, only print user if "i" is pressed, try this:
def infoCall(usr):
while True:
keyboard.wait("i"):
print(usr)