mercredi 21 février 2018

Adding two numbers using a randomised operator

The following is my code, and it works. It creates two random numbers, a random operator and puts them together, however it does not sum them after doing this. What I need is for the program to recognise it as an equation, rather than simply printing out the individual variables.

So, to avoid confusion; my question is: How would I get firstNumber and secondNumberto be summed together using whatever operator is selected, rather than simply printing them out together?

from random import choice
from random import randint

ranOperator = ["*", "/", "+", "-"]

def askQuestion():
    firstNumber = randint(1,10)
    secondNumber = randint(1,10)
    operator = choice(ranOperator)

    generateQuestion = ' '.join((str(firstNumber), operator, str(secondNumber)))
    print(generateQuestion)

askQuestion()

Current output (example):

4 + 3

Using the same numbers above, what I would want to happen:

7




Aucun commentaire:

Enregistrer un commentaire