-2

I'm new to python and coding in general. I have a problem where I don't think it's using the operator correctly. I was wondering if someone could help.

types = ("minus","times","plus","divided by")
q3type = random.choice(types)
if q3type == "minus":
    q3operator = "-"
if q3type == "times":
    q3operator = "*"
if q3type == "plus":
    q3operator = "+"
if q3type == "divided by":
    q3operator = "/"
q3a = random.randint(1,10)
q3b = random.randint(1,10)
print("What is",q3a,q3type,q3b,"?")
q3 = input("Answer: ")
correctAnswer = (q3a,q3operator,q3b)
if q3 == (correctAnswer):
    score = int(score + 1)
    print("Correct!, your score is now:",score)
else:
    print("Oops, you got that one wrong")
Foks
  • 1
  • 1
  • The problem is in this line: `correctAnswer = (q3a,q3operator,q3b)`. The left-hand side doesn't magically evaulate the expression - instead, it returns a [tuple](https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences) of strings. – Akshat Mahajan Apr 02 '16 at 05:21
  • 3
    `don't think it's using the operator correctly`. One of the biggest learning curves for new programmers is getting used to the idea that in 99.9999% of cases, when something does not work, it's because **you** are wrong and not the language or library you are trying to learn. – Mad Physicist Apr 02 '16 at 05:24

2 Answers2

2

The Solution

The problem is in these two lines:

q3 = input("Answer: ")
correctAnswer = (q3a,q3operator,q3b)

q3 gets a string. correctAnswer gets a tuple of strings. When you compare the two, they'll obviously never be equal.

The simplest way to solve this is to change lines to the following:

q3 = int(raw_input("Answer: ")) # guarantee q3 is treated as an integer
correctAnswer = eval(str(q3a)+q3operator+str(q3b)) # eval treats a string as Python command

This should solve your problem for you.

How it works

eval converts a string into a Python expression and evaluates it - something like "5 + 3" gets converted into the actual expression 5 + 3 and evaluated to yield 8.

I had to convert q3a and q3b to strings so that I could use eval on them - hence why I used str(q3a) and str(q3b).

Note that raw_input only works on Python 2.7 - input is the right thing to use in Python 3.

eval is also potentially dangerous if you can't control what you pass to it - it's good in this situation, but the fact you have to use it indicates that there's probably a much better way to write this program. :)

How you can improve your program

The key to writing good programs is to use the right language constructs for them.

In this case, you can improve your code by utilising two very powerful Python features: dictionaries and lambda functions. I'll leave you to spend some time reading on what they are and could do.

Here's an example of what your program could look like with these improvements (to keep it simple, let's say you only want simple addition and subtraction problems):

functions = {"plus": lambda x, y : x + y, "minus": lambda x, y: x - y}
operation = random.choice(functions.keys())

a,b = random.randint(1,10), random.randint(1,10)
answer = int(raw_input("What is {0} {1} {2}?".format(a,operation,b))

print("Correct!" if answer == functions[operation](a,b) else "Wrong!") 

Note how much shorter, cleaner and easier it is. You can add arbitrary functions to functions and guarantee the rest of the program works perfectly.

Moral: Use the right tool for the right job.

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
1

Here's the previous solution to your problem, including how to turn this into a quiz with multiple questions!

In your example, it would be easy for you to use python's operator module. Replace q3operator = "+" with q3operator = operator.add and so on (see previous solution). Finally, correctAnswer = (q3a,q3operator,q3b) becomes correctAnswer = q3operator(q3a,q3b). Remember to import operator at the top of your code!

You will also need to set score = 0 at some point (before you attempt to use it on the right side of score = int(score + 1).

Alex Griffis
  • 708
  • 4
  • 9