(python3中的“总计:$%。2f”(总计)

时间:2019-10-01 20:41:55

标签: python python-3.x

我不断收到语法错误:以下打印语句的语法无效:

print("Grand Total: $%.2f" (grand_total))

有人可以帮忙吗?

我没有运气就尝试了其他变化。

# Set dictionary and lists
grocery_item = {}
grocery_history = []
stop = 'go'
# Shopping list loop
while stop != 'q':
    item_name = input("Item name:\n")
    quantity = input("Quantity purchased:\n")
    cost = input("Price per item:\n")
    grocery_item['name'] = item_name
    grocery_item ['number'] = int(quantity)
    grocery_item['price'] = float(cost)
    grocery_history.append(grocery_item)
    stop = input("Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n")

grand_total = 0
# Total items/ cost
for grocery_item in grocery_history:
    item_total = grocery_item['number'] * grocery_item['price']
    # Compute grand total
    grand_total = item_total + grand_total
    # Print Gracery History List
    print(grocery_item[number] + grocery_item['name'] + " @ $%.2f" + grocery_item['price'] + " ea $%.2f" + (item_total)
    # Print Grang Total
    print("Grand Total: $%.2f" (grand_total))
Quantity purchased:
Price per item:
Would you like to enter another item?
Type 'c' for continue or 'q' to quit:
Item name:
Quantity purchased:
Price per item:
Would you like to enter another item?
Type 'c' for continue or 'q' to quit:
Item name:
Quantity purchased:
Price per item:
Would you like to enter another item?
Type 'c' for continue or 'q' to quit:
1 milk @ $2.99 ea $2.99
2 eggs @ $3.99 ea $7.98
4 onions @ $0.79 ea $3.16
Grand total: $14.13

1 个答案:

答案 0 :(得分:0)

在注释中已经指出,除了在上次调用%中丢失字符串格式运算符print之外,您还缺少了在上次对{{1 }}:

print