首页 / PYTHON / Python时间限制
Python时间限制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python时间限制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2031字,纯文字阅读大概需要3分钟。
内容图文
![Python时间限制](/upload/InfoBanner/zyjiaocheng/695/5aefda494c2d475f9b730d24aa62e50d.jpg)
我有一个作业要做,我真的需要一个解决方案.从昨天开始,我一直在尝试这样做,但是我不知道怎么做.
程序必须生成并打印字母或数字,然后用户必须尽快键入它并按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
解决方法:
这是我的方法:
import string
import random
import 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 #the variable that will hold the user's score
start = time.time() #the variable that holds the starting time
elapsed = 0 #the variable that holds the number of seconds elapsed.
while elapsed < 30: #while less than 30 seconds have elapsed
if response == c: #if the response from the previous loop matches the character
score += 1 #from the previous loop, increase the score.
#c is a random character
c = random.choice(string.ascii_lowercase + string.digits)
print(c)
response = input("Type a letter or a number: ") #get the user's response
elapsed = time.time() - start #update the time elapsed
内容总结
以上是互联网集市为您收集整理的Python时间限制全部内容,希望文章能够帮你解决Python时间限制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。