admin管理员组文章数量:1406944
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_qus
is 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_qus
is 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 |1 Answer
Reset to default 0The 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)
本文标签: pythonI am trying to stop inputtimeout in pycharm but i don39t know howStack Overflow
版权声明:本文标题:python - I am trying to stop inputtimeout in pycharm but i don't know how - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743914a2622774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for _ in range(num_qus):
– Barmar Commented Mar 12 at 17:49global num_qus
in the function. You should be getting an error from the linenum_qus -= 1
– Barmar Commented Mar 12 at 17:51