Solved- python unsupported operand type

In this article, you will learn how to solve python unsupported operand type for -: ‘int’ and ‘str’. Let’s look at a code example that produces…

The post Solved- python unsupported operand type appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

In this article, you will learn how to solve python unsupported operand type for -: ‘int’ and ‘str’.

Let’s look at a code example that produces the same error.

total_money = int(input("How much money you have? "))
Shirt_price = input("How many you bought? ")
price_pershirt = input("what is the price per shirt? ")
money_remaining = total_money - price_pershirt
print("You have ${} money left.".format(money_remaining))

Output


How much money you have? 300
How many you bought? 20
what is the price per shirt? 10
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    money_remaining = total_money - price_pershirt
TypeError: unsupported operand type(s) for -: 'int' and 'str'


Learn how to solve TypeError: unsupported operand type(s) for -: ‘int’ and ‘str’ error.

In the code snippet above we are subtracting an integer from a string which is not possible and throws the error. we can solve this error by converting the price_pershirt variable value to integer by using int() method.

total_money = int(input("How much money you have? "))
Shirt_price = input("How many you bought? ")
price_pershirt = int(input("what is the price per shirt? "))
money_remaining = total_money - price_pershirt
print("You have ${} monney left.".format(money_remaining))

output

How much money you have? 500
How many you bought? 30
what is the price per shirt? 10
You have $490 monney left.

The post Solved- python unsupported operand type appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-03-09T12:52:00+00:00) Solved- python unsupported operand type. Retrieved from https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/

MLA
" » Solved- python unsupported operand type." Deven | Sciencx - Tuesday March 9, 2021, https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/
HARVARD
Deven | Sciencx Tuesday March 9, 2021 » Solved- python unsupported operand type., viewed ,<https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/>
VANCOUVER
Deven | Sciencx - » Solved- python unsupported operand type. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/
CHICAGO
" » Solved- python unsupported operand type." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/
IEEE
" » Solved- python unsupported operand type." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/. [Accessed: ]
rf:citation
» Solved- python unsupported operand type | Deven | Sciencx | https://www.scien.cx/2021/03/09/solved-python-unsupported-operand-type/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.