2

我有一个家庭作业要做,我真的需要一个解决方案。从昨天开始我一直在尝试这样做,但我不知道如何。

程序必须生成并打印一个字母或数字,然后用户必须尽快键入它并按 ENTER。30秒后游戏结束。

好吧,我不知道如何为游戏设置时间限制。我正在通过stackoverflow搜索,但没有发现任何有用的东西。请帮我。

**这是我到目前为止所做的。我尝试了 SYSS.STDER 答案中的代码,但它并不完全有效,因为当 30 秒结束时,游戏也应该结束,但是在这段代码中,当我输入最后一个字符时游戏结束了。

LOOP 不会停止,直到它完成并且我们发现我们已经过了最后期限。时间一到,就需要立即中断正在进行的任务。

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
4

4 回答 4

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
于 2012-12-15T15:28:07.767 回答
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))
于 2012-12-17T03:38:09.430 回答
0

超过时间限制时退出素数函数的示例程序。

导入数学

从时间进口时间

start_time = time() max_time = 2

定义素数(n):

    for i in range(2, int(math.sqrt(n))):
        if(time() - start_time) > max_time:
            return "TLE"
        if n % i == 0:
            return False
    return True

打印(素数(3900000076541747077))

于 2022-02-21T09:38:15.863 回答
-2

使用 python 中可用的“timeit”模块以获得更好的结果。

于 2016-11-03T07:22:16.090 回答