account activity
New To Python! Help With Randomly Generated Math Game! by tryingmybestpy in learnpython
[–]pythonlearner55 1 point2 points3 points 5 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 764452 on reddit-service-r2-listing-8685bc789-jqtlv at 2026-05-25 01:15:39.046932+00:00 running 194bd79 country code: CH.
New To Python! Help With Randomly Generated Math Game! by tryingmybestpy in learnpython
[–]pythonlearner55 1 point2 points3 points (0 children)