0

I'm trying to implement a simple maths game where the user is given random numbers and operators then they have to work out the answer, I found sources on the internet that suggested using the operator module, which is why i used it, if there is a more efficient/ easier way of achieving this I am very open for interpretation.

Essentially i am trying to remove this horrible <built-in function add> and swap it to be more user friendly by saying '+' or 'add', something along those lines but i keep getting the error 'TypeError: 'bool' object is not callable - python', i really dont know what this means, i am new to python and am very confused.

 from operator import add, sub, mul, floordiv

 operators = (add == str("add"), sub == str("subtract"), mul == str("multiply"), floordiv == str("divide"))
 operator = random.choice(operators)
 answer = operator(number1, number2)
 question = print("What is ", number1, operator, number2, "?")
6
  • You should change your operators tuple to a dictionary as shown in this post Commented Nov 25, 2014 at 13:47
  • Not sure why you guys are downvoting this. Let the OP work out a problem definition. :) We're here to help people learn, right? We can also help them learn to ask questions, also in the space they don't really grasp yet. Commented Nov 25, 2014 at 13:50
  • Josh, can you also write out in words what you are trying to achieve? Not really clear where you are heading :) If you're trying to make a simple math game, you don't need the operator module at all. Commented Nov 25, 2014 at 13:53
  • essentially it is a simple maths game where the user is given random numbers and operators then they have to work out the answer, I found sources on the internet that suggested using the operator module, which is why i used it, if there is a more efficient/ easier way of achieving this I am very open for interpretation. sorry for the poor clarity of the question Commented Nov 25, 2014 at 13:55
  • @JoshHughes: moved your comment to the answer for clarity sake. Commented Nov 25, 2014 at 13:59

4 Answers 4

2

You want two seperate things: the string representation of the operator and the operator itself. You can use dictionary to store the function and the corresponding string representation:

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

number1 = 9
number2 = 3

operators = {'+':add, '-':sub, '*':mul, '/':floordiv}
operator_name = random.choice(operators.keys())
op = operators[operator_name]
answer = op(number1, number2)
question = "What is {}{}{}?".format(number1, operator_name, number2)
print question
print answer
Sign up to request clarification or add additional context in comments.

Comments

2

What you get as a result of the first line is

operators = (False, False, False, False, False)

or something in those lines.

Then you are trying to call a boolean that gets you the exception you have.

add == str("add") will evaluate to False, since you're trying to compare a function to a string.

I'm assuming you are trying to implement a simple math game, hence instead of using operator, which are in fact math operation functions, you can just use a simple dictonary:

operators = { 'add': add, 'substract': sub, 'multiply': mul }
answer = operators[random.choice(operators.keys())](number1, number2)

2 Comments

your last line answer = ... might need some editing to run without errors. And what about number1 and number2?
@hitzg: ah, indeed, wrong bracer, sorry ;) And I didn't provide complete working solution, just a few pointers here.
1

Here's an implementation using tuples instead of dicts, I added in more ways to represent each operation.

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

number1 = 5
number2 = 10

operators = (
    (add, ("add", "plus", "+")),
    (sub, ("subtract", "minus", "-")),
    (mul, ("multiply", "times", "*")),
    (floordiv, ("divide", "over", "/")),
)
operator, (name, operation, symbol) = random.choice(operators)
print("What is ", number1, operation, number2, "?")
print(operator(number1, number2))

Output

13:50 ~ $ python3 StackOverflow/27128400.py 
What is  5 times 10 ?
50
13:55 ~ $ python3 StackOverflow/27128400.py 
What is  5 plus 10 ?
15
13:55 ~ $ python3 StackOverflow/27128400.py 
What is  5 minus 10 ?
-5
13:55 ~ $ python3 StackOverflow/27128400.py 
What is  5 over 10 ?
0

Comments

0

What you're trying to do here is certainly possible. The formation of the tuple above does not return anything useful. A dictionary is more appropriate.

You can construct the dictionary as:

operators = {'add':add, 'minus':sub, 'multiplied by':mul, 'divided by':floordiv}

This gives you a dictionary of related strings and functions. You can choose a random operator name (this will give the string value, not the operator itself.

operator = random.choice(list(operators.keys()))

Then compute the answer:

answer = operators.get(operator)(number1, number2)

This gets the operator function from the operators dictionary by 'searching' for its string partner.

Altogether now (with some numbers so it runs off the bat):

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

operators = {'add':add, 'minus':sub, 'multiplied by':mul, 'divided by':floordiv}
operator = random.choice(list(operators.keys()))
number1 = 2
number2 = 3
answer = operators.get(operator)(number1, number2)
print "What is " + str(number1) + " " + operator + " " + str(number2) + "?"
print answer

(I'm using 2.7 for this)

2 Comments

FYI: dict.keys() returns a list. So there is no need to call list(dict.keys())
I'm at work so it's taken a while to write it, the other answers weren't there when I began. I sure hope no one has been irrecoverably harmed by it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.