0

I'm trying to make a simple calculator in Python. I'm asking for 2 numbers and then operating based on the operational character they choose. However, the variable is turned into a string type so I can't add, subtract, etc.

This is the code:

a = input("Enter first number")
print(a)
b = input("Enter second number")
print(b)
op = input("Choose operation by entering +, -, x, or /")

if(op == "+"):
    print(a+b)

elif(op == "-"):
    print(a-b)

elif(op == "x"):
    print(a*b)

elif(op == "/"):
    print(a/b)    
else:
    print("Sorry; input the correct operational character")
3
  • sorry the code looks weird in the question Commented Mar 12, 2020 at 3:35
  • 1
    Just cast a and b to int or float. Commented Mar 12, 2020 at 3:38
  • 1
    Does this answer your question? How can I read inputs as numbers? Commented Mar 12, 2020 at 3:39

1 Answer 1

2

example:

a = input("Enter first number")
a = int(a)

Another way:

a = int(input("Enter first number"))
Sign up to request clarification or add additional context in comments.

Comments

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.