最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - I am trying to stop inputtimeout in pycharm but i don't know how - Stack Overflow

programmeradmin0浏览0评论

My code is:

import random  
from inputimeout import inputimeout, TimeoutOccurred  
  
# todo inputs  
b = input("press enter to start")  
lowest_numb = input("lowest number")  
highish_numb = input("highish_numb")  
time_for_qus = int(input("time for question"))  
num_qus = int(input("How many questions?"))  
print(type(time_for_qus))  
  
def ask_question(num_qus=num_qus):  
    ran_1 = random.randint(int(lowest_numb), int(highish_numb))  
    ran_2 = random.randint(int(lowest_numb), int(highish_numb))  
    print(f"{ran_1}x{ran_2}", end="")  
    try:  
        answer = inputimeout("", time_for_qus)  
        num_qus -= 1  
    except TimeoutOccurred:  
        print("Times up!!!")  
        ask_question(num_qus)  
        if num_qus == 0:  
            quit()  
  
ask_question(num_qus)

I was expecting it to print out a times table question and stop after num_qusis equal to 0 but it just carry's on printing questions.

Can you help?

My code is:

import random  
from inputimeout import inputimeout, TimeoutOccurred  
  
# todo inputs  
b = input("press enter to start")  
lowest_numb = input("lowest number")  
highish_numb = input("highish_numb")  
time_for_qus = int(input("time for question"))  
num_qus = int(input("How many questions?"))  
print(type(time_for_qus))  
  
def ask_question(num_qus=num_qus):  
    ran_1 = random.randint(int(lowest_numb), int(highish_numb))  
    ran_2 = random.randint(int(lowest_numb), int(highish_numb))  
    print(f"{ran_1}x{ran_2}", end="")  
    try:  
        answer = inputimeout("", time_for_qus)  
        num_qus -= 1  
    except TimeoutOccurred:  
        print("Times up!!!")  
        ask_question(num_qus)  
        if num_qus == 0:  
            quit()  
  
ask_question(num_qus)

I was expecting it to print out a times table question and stop after num_qusis equal to 0 but it just carry's on printing questions.

Can you help?

Share Improve this question edited Mar 12 at 17:48 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Mar 12 at 15:36 user29961513user29961513 13 bronze badges 2
  • Aside: Don't use recursion when a loop will work easily. Use for _ in range(num_qus): – Barmar Commented Mar 12 at 17:49
  • You need global num_qus in the function. You should be getting an error from the line num_qus -= 1 – Barmar Commented Mar 12 at 17:51
Add a comment  | 

1 Answer 1

Reset to default 0

The issue with your code is that when you call ask_question(num_qus), it doesn't correctly stop when num_qus reaches 0. Instead, it recursively calls itself without decrementing num_qus properly.

import random
from inputimeout import inputimeout, TimeoutOccurred

input("Press enter to start") 
lowest_numb = int(input("Lowest number: "))
highish_numb = int(input("Highest number: "))
time_for_qus = int(input("Time for question (seconds): "))
num_qus = int(input("How many questions? "))

def ask_questions(num_qus):
    while num_qus > 0:
        ran_1 = random.randint(lowest_numb, highish_numb)
        ran_2 = random.randint(lowest_numb, highish_numb)
        print(f"{ran_1} x {ran_2} = ", end="")

        try:
            answer = inputimeout("", time_for_qus)
        except TimeoutOccurred:
            print("Time's up!!!")

        num_qus -= 1

ask_questions(num_qus)
发布评论

评论列表(0)

  1. 暂无评论