python - 实验室 : calculate discounts

标签 python python-2.x

我正在做一个练习,到目前为止,代码(在其他线程的一些帮助之后)现在工作得很好几乎,但是......无法获得正确的结果数学观点。

代码如下:

#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)

该练习要求编写一个程序来计算客户在购买两件商品后需要支付多少钱,具体取决于促销、俱乐部卡和税费。

问题在于结果。作为输入示例:

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

相反,我得到了:

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

语法有什么问题?非常感谢!

最佳答案

问题

在第二个条件中,您编写了 print('Total price = ' + totalprice) 行而不是 print('Total price = ', totalprice),并且问题在于:

totalpricefloat 类型,同时 'Total price = 'str 和你正在尝试的要做的几乎就像 str() + float(),因为 python 不知道如何连接字符串和 float ,所以会引发异常。

如何解决

1) 在所有地方使用相同的 print('Total price = ', totalprice)

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

因为 print 自动将所有内容转换为字符串表示形式,您可以想象 print('Total price = ', totalprice) 表达式是这样的:

print(str('总价 = ') + ""+ str(totalprice))

2)将float转换为str并拼接str

print('总价 = ' + str(totalprice))

str(totalprice)totalpricefloat 转换为 str 并且 python 知道如何将字符串连接在一起.

3) 格式化

"Total price = {}".format(3.14)" 等同于"Total price = 3.14" 字符串,
所以 print("Total price = {}".format(totalprice)) 也可以用

在 python 3 中我们也有 f-stings:

f"总价 = {3.14}"== "总价 = 3.14"

print(f"总价 = {totalprice}")

关于python - 实验室 : calculate discounts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69106313/

相关文章:

python - 如何防止 C 共享库在 python 的标准输出上打印?

python - 在 Python 2 中,如何写入父作用域中的变量?

python - 科学数据包 : calculate precision and recall using cross_val_score function

Python CSV 将单列读取为列表

python - 当字符串值转换为数字大于N时,Pandas如何计数?

python - 查找元素并使用 Python 将 XPath 返回给它

python - 为什么 print 在 lambda 中不起作用?

python - 有没有办法在设置大小参数时使 buffer() 可写而不进行复制?

python - 如何在 Python 的另一个函数中找出特定函数参数的默认值?

python - Numpy 根据条件拆分数组,无需 for 循环