Princey
Princey

Reputation: 1

Convert String to Int to subtract from another number. Python

I'm really not even sure about if I've worded the title correctly. Essentially I'm supposed to take a list of five products and prices, sort them in order from most to least expensive. Then take the cheapest price and subtract it from the total overall price.

I've been able to do everything except this final step. I can find the cheapest price, and the total price. I just can't subtract one from the other without an error.

Here's my code:

totalprice = (0)
allproducts = {}


for i in range (5):
    products = input("Enter product name")
    prices = float(input("Enter a product price"))
    allproducts[products]=prices
    totalprice = prices + totalprice


lowestprice = min(allproducts.items(), key=lambda x: x[1])


sortedprice = sorted(allproducts.items(), key=lambda x: x[1], reverse=False)

sortedprice.reverse()

fulltotal = totalprice - lowestprice


print("Your Items From Most to Least Expensive:", sortedprice)
print("Cheapest Item:", lowestprice)
print("Your Total Before Discount is:", totalprice)

print("Your Price With Discount is:", fulltotal)

Now if I run this code without the "fulltotal = totalprice - lowestprice, and "Print(fulltotal)", everything works fine. However when I add them, I get this in my output:

Enter product nameProduct 1
Enter a product price15
Enter product nameProduct 2
Enter a product price20
Enter product nameProduct 3
Enter a product price10
Enter product nameProduct 4
Enter a product price25
Enter product nameProduct 5
Enter a product price5
Traceback (most recent call last):
  File "C:\Users\fatel\Documents\Assessment2.py", line 19, in <module>
    fulltotal = totalprice - lowestprice
TypeError: unsupported operand type(s) for -: 'float' and 'tuple'

Upvotes: 0

Views: 62

Answers (4)

constantstranger
constantstranger

Reputation: 9379

The issue is that lowestprice is a (key, value) tuple.

The reason is that items() is designed to iterate through the (key, value) tuples of the dictionary. This is the same reason you needed to use key=lambda x: x[1] to get at the value within each (key, value) tuple yielded by items() in min() and sorted().

Accessing index 1 of the tuple returned by min() would fix your code:

lowestprice = min(allproducts.items(), key=lambda x: x[1])[1]

However, a more streamlined approach might be to use values() instead of items() throughout your example:

lowestprice = min(allproducts.values())
sortedprice = sorted(allproducts.values(), reverse=True)
fulltotal = totalprice - lowestprice

The values() method lets you iterate over value only (no key), which seems to be what's really relevant in your case.


Here's an overview of items(), values() and keys() which are known as "dictionary view objects": https://docs.python.org/3/library/stdtypes.html#dict-views

Upvotes: 2

Maxime Bouhadana
Maxime Bouhadana

Reputation: 458

I think you had an issue with the type of lowestprice When I ran the code it appeared that its type was a tuple. Therefore it can't be substracted with the total price.

Try and run with this line instead :

lowestprice = min(allproducts.values())

Cheers !

Upvotes: 1

Eshaan Gupta
Eshaan Gupta

Reputation: 614

Your lowestprice was a tuple, not a string or float. Here, I have picked the 1st element from the tuple, which is the lowest price.

totalprice = 0
allproducts = {}


for i in range (5):
    products = input("Enter product name")
    prices = float(input("Enter a product price"))
    allproducts[products]=prices
    totalprice = prices + totalprice


lowestprice = min(allproducts.items(), key=lambda x: x[1])[1] #Added [1]


sortedprice = sorted(allproducts.items(), key=lambda x: x[1], reverse=False)

sortedprice.reverse()

fulltotal = totalprice - lowestprice


print("Your Items From Most to Least Expensive:", sortedprice)
print("Cheapest Item:", lowestprice)
print("Your Total Before Discount is:", totalprice)

print("Your Price With Discount is:", fulltotal)

Upvotes: 0

Sean
Sean

Reputation: 552

lowestprice = min(allproducts.items(), key=lambda x: x[1]) is returning a tuple (product_name, product_price). lowestprice = min(allproducts.items(), key=lambda x: x[1])[1] will return just the price and then everything works.

Upvotes: 0

Related Questions