I Have an error in my python calc project (Can anyone help me, please?)

Hi all,

I was wondering if anyone can help with this calculator project I’m working on.
It shows I have an error with subtracting two numbers in line 8.
Here is the code :
num1 = input("Enter a number : ")
sym1 = input("Enter a math symbol (+ , - ,* ,/) : ")
num2 = input("Enter another number : ")

if sym1 == '+':
(num1 + num2)
if sym1 == '-':
(num2 - num1)
if sym1 == '-':
(num1 - num2)
if sym1 == '*':
(num1 * num2)
if sym1 == '/':
(num1 / num2)

sum = float(num1)+float(num2)
sum1 = float(num1)*float(num2)
sub = float(num2)-float(num1)
sub1=float(num1)-float(num1)
div = float(num1)/float(num2)

print("The {0} of {1} and {2} is {3}" .format(num1, num2, sym1, sum, sum1, sub, sub1, div ))
And here is the error:
Traceback (most recent call last):
File "C:\Users\(some_user)\AppData\Local\Programs\Python\Python39-32\(some_project)", line 8, in <module>
(num2 - num1)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Can anyone view this code and tell me the error in line 8, please?
(Anyone can tell the answer by replying to this post.)

Thanking you all in advance.

Regards,
Vaibhav Chakravarthi

I was wondering if anyone can help with this calculator project I’m
working on.
It shows I have an error with subtracting two numbers in line 8.
Here is the code :
num1 = input("Enter a number : ")

Those of us in email land will find things easier to read if you just
put your code indented by 4 spaces, and no backtick surrounds.

sym1 = input("Enter a math symbol (+ , - ,* ,/) : ")
num2 = input("Enter another number : ")
[…]
`print(“The {0} of {1} and {2} is {3}” .format(num1, num2, sym1, sum,

You’ll find this:

print(f"The {num1} of {num2} and {sym1} is {sum}.")

easier to get right, and to correct.

And here is the error:
Traceback (most recent call last):
File "C:\Users\(some_user)\AppData\Local\Programs\Python\Python39-32\(some_project)", line 8, in <module>
(num2 - num1)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Can anyone view this code and tell me the error in line 8, please?

The input() function returns a string. You’re trying to do arithmetic,
so you need to convert that string to an int, eg:

num1 = int(input(......))

Cheers,
Cameron Simpson cs@cskk.id.au

Hi Cameron,

Thank you very much for the answer!
I’d love to answer any of the questions of yours.
just let me know :wink:.

Thanking you in advance.

Regards,
Vaibhav Chakravarthi