python 时间限制_Python时间限制

我有一个作业要做,我真的需要一个解决方案.从昨天开始,我一直在尝试这样做,但是我不知道怎么做.

程序必须生成并打印字母或数字,然后用户必须尽快键入它并按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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值