为什么当我尝试写入百分比时它显示"Expected expression"?



这是制作包含不同商品的发票的实践代码,其中4种产品定价中的每一种商品的GST都不同。

product = input('name product')
price = input('give price')
if product == 'footwear':
if price <= 500:
x = 5%        
if (product == 'footwear'):
if price > 500:
x = 18% 
if product == 'apparels':
if price <= 1000:
x = 5%           
if product == 'apparels':        
if price <= 1000:
x = 12%         

print('item:', product)
print('price:', price)
print('GST:', price * x)
print('total:', (price * x) + price)

我只是在学习,但我不知道为什么它会显示出"应为表达式";。

在编写5%时,实际上使用的是需要第二个参数的mod运算符。

运算符%将返回2个数字之间的除法的余数。因此,6%3将返回0,7%3将返回1,8%3将返回2,等等。

要保存实际百分比,可以使用十进制数字,例如5%:0.05

相关内容

最新更新