python打開文件,python等待用戶輸入指定秒_如何在10秒后強制用戶輸入

 2023-10-04 阅读 28 评论 0

摘要:我是一個python初學者,需要一些幫助來進行類似測驗的游戲。這是我的代碼:import timefrom threading import Timerimport random as rndpython打開文件,q = ["q1", "q2", "q3"]a = ["a1 b1 c1", "a2 b2 c2",

我是一個python初學者,需要一些幫助來進行類似測驗的游戲。

這是我的代碼:import time

from threading import Timer

import random as rnd

python打開文件,q = ["q1", "q2", "q3"]

a = ["a1 b1 c1", "a2 b2 c2", "a3 b3 c3"]

ca = ["b", "c", "b"]

points = 0

rand_q = rnd.randint(0, len(q) - 1) # Choosing random question

python enumerate?print(q[rand_q] + "\n" + a[rand_q] + "\n") # Asking question and showing answers

time.sleep(0.5) # Little pause between prompts

t = Timer(10, print, ['Time is up!']) # Setting up timer

t.start() # Start timer

start = time.time() # Start of time check

python input?answer = input("You have 10 seconds to choose the correct answer.\n") # User input

if answer is ca[rand_q]: # Check if answer is correct

print("Correct answer!")

points = (points + round(10 - time.time() + start, 1)) * 10 # Calculate points

else:

python編程100例。print("Wrong answer!")

t.cancel() # Stop timer

print("Points:", points)

input("Press ENTER to quit")

del q[rand_q] # Removing the question

python編程?del a[rand_q] # Removing the answer

del ca[rand_q] # Removing the correct answer

當我運行這個程序時,我可以回答問題并得到分數,但當我等待計時器時,我會得到一個提示,表示時間到了,但我仍然可以填寫并回答問題。在

我希望輸入在10秒后停止工作,但我似乎無法使此工作。有什么方法可以讓定時器超時所有以前的輸入在“時間到了”的提示。在

我見過更多這樣的帖子,但它們似乎過時了,我沒能讓它們發揮作用。在

qpython、編輯:睡眠命令不起作用。它會打印一行,說明已經太晚了,但您仍然可以在之后輸入答案。線程計時器也是如此。我想在10秒后終止輸入命令,但是對于windows似乎沒有解決方案。在

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/113312.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息