0

I am trying to code a simple calculator but I am having a problem. I can't seem to add, subtract, multiply, or divide two variables and print them. I am trying to code so the user can input the numbers along with the operator. Any ideas?

Thanks,

SanguineL

number1 = raw_input ("What is the first number?")
operator = raw_input ("What is the operator?")
number2 = raw_input ("What is the second number?")

elif operator == +
    answer = number1 + number2
elif operator == -
    answer = number1 - number2
elif operator == *
    answer = number1 * number2
elif operator == /
    answer = number1 / number2
print answer
2
  • This is not at all valid python code. Please to a basic python tutorial first. Commented May 2, 2018 at 16:28
  • I assume the error you get when you run your program is a syntax error, and you need help fixing that? Or are you asking about something else? Commented May 2, 2018 at 16:28

3 Answers 3

1

You need to distinguish the string you get from raw_input from the function an operator represents.

if operator == "+":
    answer = number1 + number2
elif ...

The operator module lets you build a dictionary that abstracts away the lookup process:

import operator
number1 = raw_input ("What is the first number?")
op = raw_input ("What is the operator?")
number2 = raw_input ("What is the second number?")
funcs = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.div}

try:
    f = funcs[op]
except KeyError:
    raise ValueError("Undefined operator %s" % (op,))
answer = f(number1, number2)
Sign up to request clarification or add additional context in comments.

Comments

0

So there's a couple issues with your code, the first issue is that raw_input will always assume the input is a string and so your number1, operator, and number2 objects will be strings (and I assume you only want the operator variable to be a string). If you want to convert your numbers to floats, then you need to write something like number1 = float(raw_input ("What is the first number?"))

The second issue is that you need to start an if block with an if statement not an elif statement. elif statements come only after the if statement since it stands for "else if" - i.e. in the form if something else if something.

The third issue is that you didn't put quotes around your operators in your conditional statements. Python will not automatically assume they are strings. Python will assume they are variables which you haven't declared yet. You should have a statement like elif operator == '-' in order to make the comparison valid.

The fourth and last issue (that I see) is that since you are using raw_input it appears you are using python 2. Python 2 has a weird behavior when using the division operator / - namely it floors the division if the inputs to it are ints or longs. That behavior can cause a lot of headaches if you are not aware of it. You should probably include a from __future__ import division line at the beginning of your code so dividing doesn't floor the answer.

Comments

0

Don't know what does raw_input ("What is the first number?") mean (assume that is some kind of form input or sdt_in), but lower part might be (don't forget to convert your inputs like this number1 = int(number1)):

if operator == '+'
    answer = number1 + number2
elif operator == '-'
    answer = number1 - number2
elif operator == '*'
    answer = number1 * number2
elif operator == '/'
    answer = number1 / number2
print(answer)

2 Comments

raw_input was the function in Python 2 that was replaced by input in Python 3.
ah, ok, never used Python 2 :)

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.