Can't find a way to incorporate addition, division, subtraction and multiplication inside of my small game

Question:

I am trying to use the variables user_ans_a, user_ans_s, user_ans_m and user_ans_d inside of my game as questions randomly chosen by python. My dilemma is coming from matching the randomly chosen variable to an answer coresponding to that variable. I want the user to be able to enter an anser to those questions.

import random
import re

while True:
    score = 0
    top = "---   Mathematics Game   ---"
    print(len(top) * "━")
    print(top)
    print(len(top) * "━")
    print("    Type 'Play' to play")

    start_condition = "Play" or "play"

    def checkint(input_value):
        while True:
            try:
                x = int(input(input_value))
            except ValueError:
                print("That was a bad input")
            else:
                return x
            

    while True:      
        inp = input()  
        if inp == "Play":  
            break
        else:
            print("Try again")

    if inp == "Play":
        print("What's your name?")
        name = input()
        print(f"Hello {name}")  

    while True:
        try:
            no_of_rounds = int(input("how many rounds do you want to play? "))
            break
        except ValueError:
            print('Please enter a number.')

    for i in range(no_of_rounds):
        ran_int1 = (random.randint(1,10))
        ran_int2 = (random.randint(1,10))
        answer_a = (ran_int1 + ran_int2)
        answer_s = (ran_int1 - ran_int2)
        answer_m = (ran_int1 * ran_int2)
        answer_d = (ran_int1 / ran_int2)
        user_ans_a = (f"What does {ran_int1} + {ran_int2} = ")
        user_ans_s = (f"What does {ran_int1} - {ran_int2} = ")
        user_ans_m = (f"What does {ran_int1} * {ran_int2} = ")
        user_ans_d = (f"What does {ran_int1} / {ran_int2} = ")

        if checkint(user_ans_a) == int(answer_a):
            print(f"That was correct {name}.")
            score = score+1
        else:
            print(f"Wrong {name}, the answer was {answer_a}, try again")

    print(f"Score was {score}")

    while True:
        answer = str(input('Run again? (y/n): '))
        if answer in ('y', 'n'):
            break
        print("invalid input.")
    if answer == 'y':
        continue
    else:
        print("Goodbye")
        break
Asked By: doman

||

Answers:

I tried running your code, and it worked fine. It only asks for addition, but thats also the only question the code uses.
To have random operations, you could for example generate another int between 0 and 3 and then choose the question depending on the number.
But in the state it is currently in, i don’t see any problems.

Answered By: Jon_Kle
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.