Python时间限制

时间:2012-12-15 14:59:32

标签: python python-3.x

我要完成一项家庭作业,我真的需要一个解决方案。我从昨天起就一直试图这样做,但我不知道怎么做。

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

3 个答案:

答案 0 :(得分:4)

这是我的方法:

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

答案 1 :(得分:0)

由于您使用的是Windows,因此可以使用msvcrt.kbhit函数检查计时循环内的按键:

import msvcrt #### windows only ####
import os
import random
import string
import time

max_time = 15

def readch(echo=True):
    "Get a single character on Windows"
    ch = msvcrt.getch()
    while ch in b'\x00\xe0': # special function key?
        msvcrt.getch()       # get keycode & ignore
        ch = msvcrt.getch()
    if echo:
        msvcrt.putch(ch)
    return ch.decode()

def elapsed_time():
    global start_time
    return time.time() - start_time

number = 0
score = 0
start_time = time.time()  # initialize timer

while elapsed_time() < max_time:
    c = random.choice(string.ascii_lowercase + string.digits)
    print(c, end='')
    number += 1
    print("Type a letter or a number: ", end='')

    while elapsed_time() < max_time:
        if not msvcrt.kbhit():
            time.sleep(0.1) # don't hog processor
        else:
            response = readch(echo=False) # get the user's response
            if response == c:
                print(response) # only print if correct
                score += 1
                break
    else:
        print()

print()
print("Time's up")
print("You go  {} out of {}:".format(score, number))

答案 2 :(得分:-2)

使用&#34; timeit&#34; python中提供的模块可以获得更好的结果。