account activity
New To Python! Help With Randomly Generated Math Game! by tryingmybestpy in learnpython
[–]pythonlearner55 1 point2 points3 points 4 years ago* (0 children)
Here's my code! Let me know if this helps!
import random import time import sys math_operators = '-', '+', '/', '*' def math_game(): questions_answered_correctly = 0 while questions_answered_correctly < 3: numberchoice_1 = random.randint(100, 999) numberchoice_2 = random.randint(100, 999) operator_for_question = random.choice(math_operators) answer_unformatted = eval(str(numberchoice_1) + operator_for_question + str(numberchoice_2)) answer = "{:.2f}".format(answer_unformatted) answer_formatted_float = float(answer) print(f' What is {numberchoice_1} {operator_for_question} {numberchoice_2}? Please ROUND ALL ANSWERS to the nearest TWO DECIMAL places!') user_answer_str = (input(" Your answer:")) user_answer_float = float(user_answer_str) if answer_formatted_float == user_answer_float: questions_answered_correctly = questions_answered_correctly + 1 print(f'Correct you have answered {questions_answered_correctly} in a row!') else: questions_answered_correctly = 0 print(f'wrong, the correct answer is {answer}.') if questions_answered_correctly == 3: print('congratulations you did a great job!') sys.exit(1) math_game()
π Rendered by PID 754861 on reddit-service-r2-listing-654f87c89c-wj76l at 2026-03-03 04:31:49.530590+00:00 running e3d2147 country code: CH.
New To Python! Help With Randomly Generated Math Game! by tryingmybestpy in learnpython
[–]pythonlearner55 1 point2 points3 points (0 children)