程序删除最便宜的项目,并显示从高到低



我正在编写一个程序,该程序将询问5个项目和每个项目的价格-我创建了两个列表,并要求按最昂贵的顺序对这些列表进行排序。然后我需要显示商品的总数,去掉最便宜的(折扣),然后显示新的金额。

我无法让总数只显示小数点后两位。这是一个新手,非常感谢任何帮助。

到目前为止的代码

#Program to work out total bill applying a discount
#create empty list
item = []
price = []
#How many items does the customer have
n = int(input("How many items does the customer have? "))
#input item and price
for i in range(n):
item.append(input("Please enter item description: "))
price.append(float(input("Please enter the price £")))
#print(item, price)
#sort list
for x in range(len(price)):
for y in range(len(price)-1):
if price[y] > price[+1]:
item[y], item[y+1] = item[y+1], item[y]
price[y], price[y+1] = price[y+1], price[y]
#display items
for x in range(len(price)):
print(item[x], price[x])
#calculate discounts
print("Total bill = ", sum(price))
#del min(price): cant get this removed 
print("Total after discount = ", sum(price))

您所需要的只是在打印语句中添加一些格式。

例如:

print("Total bill = %.2f" % sum(price))
print("Total after discount = %.2f" % sum(price))

%.2f是一条将float格式化为小数点

后面只有2个整数的指令。一种完全相同的替代方法是使用f-string

print(f"Total bill = {sum(price):.2f}")
print(f"Total after discount = {sum(price):.2f}")