Solved- python unsupported operand type
Posted on: March 09, 2021 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.
Share on social media
//