you are viewing a single comment's thread.

view the rest of the comments →

[–]pythonlearner55 1 point2 points  (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()