python 回合函数下的循环时间React博弈

siotufzp  于 2023-03-16  发布在  Python
关注(0)|答案(1)|浏览(86)
import random



print("Welcome to Response Time Tester!")
print("You will be asked to press Enter after 
random amounts of time.")
print("How close can you get? ")  
while True:
    try:
        rounds = int(input("Enter desired number 
of rounds: "))
if rounds <= 0:
        print("Number of rounds must be greater 
than zero. Try again.")
    else:
        break
except ValueError:
    print("Invalid input. Enter a number.")

num = random.randint(2, 8)    

for rounds in range(1, rounds+1):
    print(f"Round {rounds} of {rounds}.")

    import time

import time
from typing import List

def calculate_rating(diff: float) -> str:
    if diff <= 0.25:
        return "Excellent!"
    elif diff <= 0.5:
        return "Great"
    elif diff <= 1:
        return "Good!"
    elif diff <= 2:
        return "OK!"
    else:
        return "Miss"

# randomly select target number of seconds between 
2 and 8 (inclusive)
target_seconds = random.randint(2, 8)
print(f"Press Enter in {target_seconds} seconds.")

# wait for 3 seconds before starting the timer
for i in range(3, 0, -1):
    print(i)
    time.sleep(1)

print("Go!")

# record the start time
start_time = time.time()

# wait for the user to press Enter
input()

# calculate the response time and the difference 
from the target time
response_time = round(time.time() - start_time, 2)
diff = abs(target_seconds - response_time)

# calculate the rating based on the difference and 
print it
rating = calculate_rating(diff)
print(f"Response time: {response_time} seconds.")
print(f"Target was {target_seconds} seconds, and 
your difference was {diff} seconds.")
print(f"Your rating is {rating}.")

这是我希望我的代码如何运行
欢迎使用响应时间测试器!在随机的时间之后,你将被要求按回车键。你能达到多接近?
输入所需的回合数:2第1轮,共2轮,3秒后按下回车键321开始!
响应时间:3.72秒,目标是3秒,你的差距是0. 72000000000002秒你的评分是好!
第2轮(共2轮)等.....

8xiog9wr

8xiog9wr1#

只需将游戏逻辑放入函数game并在循环中运行它:

import time
import random

def calculate_rating(diff: float) -> str:
    if diff <= 0.25:
        return "Excellent!"
    elif diff <= 0.5:
        return "Great"
    elif diff <= 1:
        return "Good!"
    elif diff <= 2:
        return "OK!"
    else:
        return "Miss"

def game():
    # randomly select target number of seconds between 2 and 8 (inclusive)
    target_seconds = random.randint(2, 8)
    print(f"Press Enter in {target_seconds} seconds.")

    # wait for 3 seconds before starting the timer
    for i in range(3, 0, -1):
        print(i)
        time.sleep(1)

    print("Go!")

    # record the start time
    start_time = time.time()

    # wait for the user to press Enter
    input()

    # calculate the response time and the difference from the target time
    response_time = round(time.time() - start_time, 2)
    diff = abs(target_seconds - response_time)

    # calculate the rating based on the difference and print it
    rating = calculate_rating(diff)
    print(f"Response time: {response_time} seconds.")
    print(f"Target was {target_seconds} seconds, and your difference was {diff} seconds.")
    print(f"Your rating is {rating}.")

print("Welcome to Response Time Tester!")
print("You will be asked to press Enter after random amounts of time.")
print("How close can you get? ")
while True:
    try:
        rounds = int(input("Enter desired number of rounds: "))
        if rounds <= 0:
            print("Number of rounds must be greater than zero. Try again.")
        else:
            break
    except ValueError:
        print("Invalid input. Enter a number.")

for rounds in range(1, rounds + 1):
    print(f"Round {rounds} of {rounds}.")
    game()

相关问题