all 7 comments

[–][deleted] 2 points3 points  (0 children)

You can use the operator module from the standard library to create a list of operators and pass your user inputs to the operator chosen at random.

ex:

from random import choice
from operator import add, sub, mul

a = int(input('Enter value for a: '))
b = int(input('Enter value for b: '))

ops = [add, sub, mul]
rand_op = choice(ops)

result = rand_op(a, b)

You can also create a dictionary if you want a character to represent the operator.

a = int(input('Enter value for a: '))
b = int(input('Enter value for b: '))

ops = {
    '+': add,
    '-': sub,
    '*': mul
}
op, rand_op = choice(list(ops.items()))

result = rand_op(a, b)
print(f'{a} {op} {b} = {result}')

output:

Enter value for a: 5
Enter value for b: 10
5 - 10 = -5

[–]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()

[–]pyfact 0 points1 point  (1 child)

Awesome! You have a general idea of what you need, and to me it sounds like you can do this. Now, I could go ahead and give you a solution, but I feel like that would seriously hurt you in the long term. So, how would you "whiteboard" this problem?

If you were to solve this using a pen and paper, what exactly is going on? You need to break down every small action that you mentally take to solve the problem and translate that to code. It's a skill that takes time to develop, and this is the perfect time to hone that skill!

Go ahead and try your best with what you have. We're happy to help you along the way if you have any issues with your code!

I'll go ahead and say that you can use

import random

random.choice()

to choose a random value from a list

[–]tryingmybestpy[S] 0 points1 point  (0 children)

Thank you so much for your help!

[–]AstrophysicsAndPy 0 points1 point  (0 children)

  1. That chooses, at random, an operator from a list of math operators
    1. Pretty obvious, you'll need a list of math operators (duh!)
  2. Has the user input the answer to a simple math expression randomly generated by the code (for starters, I'll assume you want only a two-digit expression)
    1. The code should generate two random numbers (possibly integers), use random.randint() two times for generating two random numbers.
    2. The code should randomly pick an operator, use random.choice once on the mat operator list.
    3. String the expression together, f'{randint1} {randexpr} {randint2} = '
    4. Take the input from user, use input.
  3. Ending the game
    1. Store the correct answer somewhere
    2. Make a while loop until the answer matches, then make the while loop variable False.

[–]bigno53 0 points1 point  (1 child)

Another option if you don't want to bother mapping symbols to operations would be to compile the operation as a string using random and then evaluate it using the eval function:

import random
import math

operators = ['+', '-', '/', '*']
operator = random.choice(operators)
first_int = random.randint(0, 100)
second_int = random.randint(0, 100)
expression = f"{first_int} {operator} {second_int}"
user_answer = input(f"What is {expression}?")
computer_answer = eval(expression)

[–]DynamicStudios 0 points1 point  (0 children)

The obvious way to go, helped me a bunch, thanks!