检测到键后,多次执行一个循环

发布于 2025-02-04 21:57:36 字数 2474 浏览 4 评论 0原文

我有代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2025-02-11 21:57:36

如果我正确理解,则应在与print()的相同级别上添加break。这种行为的原因可能是printis_pressed快得多。

def infoCall(usr):
    while True:
        if keyboard.is_pressed("i"):
            print(usr)
            break

如果您希望它永远继续下去,则仅在按下“我”时打印用户,请尝试以下操作:

def infoCall(usr):
    while True:
        keyboard.wait("i"):
        print(usr)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文