我有一个作业要做,我真的需要一个解决方案.从昨天开始,我一直在尝试这样做,但是我不知道怎么做.
程序必须生成并打印字母或数字,然后用户必须尽快键入它并按Enter. 30秒后游戏结束.
好吧,我不知道该如何限制比赛时间.我正在搜索stackoverflow,但没有发现任何有用的东西.请帮我.
**这是我到目前为止所做的.我从SYSS.STDER的答案中尝试了代码,但是由于30秒结束时,游戏也应该结束,因此它不能正常工作,但是在此代码中,当我键入最后一个字符时,游戏结束了.
循环不会停止,直到完成为止,我们发现我们已逾期.任务需要随着时间流逝而不断地进行.
max_time =30
start_time = time.time() # remember when we started
while (time.time() - start_time) < max_time:
response = "a" # the variable that will hold the user's response
c = "b" #the variable that will hold the character the user should type
score = 0
number = 0
c = random.choice(string.ascii_lowercase + string.digits)
print(c)
number = number + 1
response = input("Type a letter or a number: ") #get the user's response
if response == c and (time.time() - start_time) < max_time:
# if the response from the previous loop matches the character
# from the previous loop, increase the score.
score = score + 1