-2

Im trying to make a really simple text calculator but I keep running into this problem.

Here is my code:

num1 = input("Enter in the first number")
num2 = input("Enter in the second number")
sign = input("Enter in the calculator operator you would like")

elif sign = "+":
   print(num1 + num2)
elif sign = "-":
   print(num1 - num2)
elif sign = "*":
   print(num1*num2)
elif sign = "/":
   print(num1/num2)

Sorry im new to python...

Sar17
  • 35
  • 6
  • My bug is that it keeps telling me the difference between int and str.. but im quite confused. – Sar17 Jan 23 '20 at 03:12
  • Maybe read numbers as integers? *".. I keep running into this problem."* does not help us understand your problem. You also have an `elif` without `if`, and you don't use equality operator to check equals. – Austin Jan 23 '20 at 03:12
  • This should not have been closed; There are multiple issues. Just fixing `input` as `int()` does not resolve the users issues. – PacketLoss Jan 23 '20 at 03:18
  • @PacketLoss That a question has multiple issues does not imply that it does not close, the first is a duplicate and the second a typo. Do you think this question will serve the community? – eyllanesc Jan 23 '20 at 03:25
  • @eyllanesc The question likely does not serve much purpose outside of this issue. However it is a good starting point to help the poster learn. My reference was the question is closed as a `duplicate` to `int(input())` when however, that answer will not fix the issues the poster is having. It is part of the issue, however the code still fails with that resolved. – PacketLoss Jan 23 '20 at 03:27
  • 1) change to `numX =float(input("Enter in the first number"))`, 2) change to `if sign == "Y": – eyllanesc Jan 23 '20 at 03:27
  • @PacketLoss For the typo just one comment is enough as the SO rules indicate. For a question to remain open it must contribute to the community, in this case the 2 problems do not contribute since the first is a known error (it has n duplicates) and the second is a typo. – eyllanesc Jan 23 '20 at 03:31

3 Answers3

2

The reason why your code is not working is because you are simply multiplying/dividing/adding/subtracting your two numbers that are as of now declared as a string.

In python, you cannot add/subtract/multiply/divide strings as an integer. You need to declare your num1 and num2 as integers.

num1 = int(input("Enter in your first number"))
num2 = int(input("Enter in your second number"))
sign = input("Enter in the calculator operator you would like")

if sign == "+":
   print(num1 + num2)
elif sign == "-":
   print(num1 - num2)
elif sign == "*":
   print(num1*num2)
elif sign =="/":
   print(num1/num2)
  • The code will still fail due to syntax and operator errors. However this does solve part of the problem. – PacketLoss Jan 23 '20 at 03:19
  • 2
    Remember to also compare your if statements with two equal signs "==" or else it will not work. Your first if statement is starting with an "elif". Remember to make it "if". – ScarletCoder16 Jan 23 '20 at 03:20
1

You have quite a few syntax errors in your code, check the comments to see where you could improve, with some reading material at the bottom!

num1 = int(input("Enter in the first number")) # You need to cast your input to a int, input stores strings.
num2 = int(input("Enter in the second number")) # Same as above, cast as INT
sign = input("Enter in the calculator operator you would like")

# You cannot use `elif` before declaring an `if` statement. Use if first!

if sign == "+": # = will not work, you need to use the == operator to compare values
   print(num1 + num2)
elif sign == "-": # = will not work, you need to use the == operator to compare values
   print(num1 - num2)
elif sign == "*": # = will not work, you need to use the == operator to compare values
   print(num1*num2)
elif sign == "/": # = will not work, you need to use the == operator to compare values
   print(num1/num2)

Code will work fine with these changes, however you should read up on Python Syntax and Operators!

Happy learning!

PacketLoss
  • 5,561
  • 1
  • 9
  • 27
1

You are getting this error because input by default gives a String. You have to convert it into int before using it.

num1 = int(input("Enter in the first number"))
num2 = int(input("Enter in the second number"))
sign = input("Enter in the calculator operator you would like")

if sign == "+":
   print(num1 + num2)
elif sign == "-":
   print(num1 - num2)
elif sign == "*":
   print(num1*num2)
elif sign == "/":
   print(num1/num2)
Tanvi Jain
  • 113
  • 1
  • 14