Код: Выделить всё
The Mad Minute
[A] Randomly generate a bunch of different math questions
[B] User cannot continue a question until the correct answer has been made
[C] User have 60 seconds to solve 10 math problems
[D] Add 1 second to the final time for every wrong answer
import random
import time
OPERATORS = ["+", "-", "*"]
MIN_OPERAND = 3
MAX_OPERAND = 9
TOTAL_PROBLEMS = 5
def generateProblem():
left = random.randint(MIN_OPERAND, MAX_OPERAND)
right = random.randint(MIN_OPERAND, MAX_OPERAND)
operator = random.choice(OPERATORS)
expression = f"{left} {operator} {right}"
answer = eval(expression)
return expression, answer
incorrectAnswer = 0
input("Press enter to start!")
print("---------------------")
startTime = time.time()
loop = 0
while time.time() - startTime < 60 and loop < TOTAL_PROBLEMS:
for i in range(TOTAL_PROBLEMS):
expression, answer = generateProblem()
while True:
userInput = input("Problem #" + str(i + 1) + ": " + expression + " = ")
if userInput == str(answer):
loop += 1
break
incorrectAnswer += 1
print("Time's up!")
# Add any incorrect answer penalization to the total time
endTime = time.time()
totalTime = endTime - startTime
totalTime = "{:.2f}".format(totalTime)
finalTime = float(totalTime) + incorrectAnswer
print("---------------------")
print("You completed The Mad Minute in", totalTime, "seconds with", incorrectAnswer, "incorrect answers! Your final time is", finalTime)
Подробнее здесь: https://stackoverflow.com/questions/781 ... en-reached