Python error: unsupported operand type(s) for -: 'str' and 'str'

0 votes

Hi. I am new to Python and I am practising to code. But I am facing some issues.

>>> a=input('>')

>3

>>> b=input('>')

>5

>>> print(b-a)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for -: 'str' and 'str'

>>>

Please help me solve this error

May 12 in Python by Tina
225 views

1 answer to this question.

0 votes

Hi. This error is caused because you are trying to do a subtraction between two strings. When you take the input, the input data is stored as a string by default. To apply subtraction, you can change the type into int(or float)

print(int(b)-int(a))
answered May 12 by Anjali

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

What's the canonical way to check for type in Python?

To check if o is an instance ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,160 points
23 views
0 votes
1 answer

When I create and remove files rapidly on windows using python I get WindowsError (Error 5)

Here's the short answer: disable any antivirus or ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
84 views
0 votes
1 answer

Python error: No handlers could be found for logger “xhtml2pdf”

The only solution is to define a ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 56,160 points
132 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 134 views
+4 votes
6 answers
0 votes
1 answer
0 votes
1 answer

How to write data to a file in Python?

Refer to the below code. data=’whatever your data ...READ MORE

answered May 12 in Python by Shaam
17 views