Lab: calculate discounts

Viewed 74

I'm doing an exercise and so far so good as the code (after some help from other threads) now works almost fine, but...can't get the right results as a math point of view.

Here it's the code:

#getting base prices from user
item1 = float(input('Enter the price of the first item: '))
item2 = float(input('Enter the price of the second item: '))
clubc = raw_input('Does customer have a club card? (Y/N): ')
tax = float(input('Enter tax rate, e.g. 5.5 for 5.5% tax: '))
basep = (item1 + item2)
print('Base price = ', basep)

#setting variables for calculation
addtax = (1 + (tax / 100))

#conditions for output
if item1 >= item2 and clubc == 'N':
    priceafterd = float(item1 + (item2 / 2))
    print('Price after discounts = ', priceafterd)
    totalprice = (priceafterd * addtax)
    print('Total price = ', totalprice)
elif item2 >= item1 and clubc == 'N':
    priceafterd = float(item2 + (item1 / 2))
    print('Price after discounts = ', priceafterd)
    totalprice = (priceafterd * addtax)
    print('Total price = ', totalprice)

if item1 >= item2 and clubc == 'Y':
    priceafterd = float((item1 + (item2 / 2)) * 0.9)
    print('Price after discounts = ', priceafterd)
    totalprice = (priceafterd * var3)
    print('Total price = ' + totalprice)
else:
    priceafterd = float((item2 + (item1 / 2)) * 0.9)
    print('Price after discounts = ', priceafterd)
    totalprice = (priceafterd * var3)
    print('Total price = ' + totalprice)

The exercise requires to write a program that computes how much a customer has to pay after purchasing two items, depending on a promo, club card and taxes.

The problem is with the results. As an example of inputs:

Enter price of the first item: 10
Enter price of the second item: 20
Does customer have a club card? (Y/N): y
Enter tax rate, e.g. 5.5 for 5.5% tax: 8.25
Base price = 30.00
Price after discounts = 22.50
Total price = 24.36

Instead, I got:

line 33, in <module>
print('Total price = ' + totalprice)
TypeError: cannot concatenate 'str' and 'float' objects

What's wrong with the syntax? Thanks a lot!

1 Answers

The problem

In the second conditional you wrote print('Total price = ' + totalprice) line instead of the print('Total price = ', totalprice), and the problem is in that:

totalprice has float type, meanwhile 'Total price = ' is the str and what you are trying to do is almost like str() + float(), and because python doesn't know how to concatenate string and float number it raises an exception.

How to solve

1) Use the same print('Total price = ', totalprice) line everywhere

Why does it work and print('Total price = ' + totalprice) does not?

Because print automatically converts everything to string representation, you can imagine print('Total price = ', totalprice) expression like that:

print(str('Total price = ') + " " + str(totalprice))

2) Convert float to str and concatenate strs

print('Total price = ' + str(totalprice))

str(totalprice) converts totalprice from float to the str and python knows how to concatenate strings together.

3) Formatting

"Total price = {}".format(3.14)" is equivalent to the "Total price = 3.14" string,
so print("Total price = {}".format(totalprice)) also will work

in python 3 we also have f-stings:

f"Total price = {3.14}" == "Total price = 3.14"

print(f"Total price = {totalprice}")

Related