Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python (“总计:$%.2f”(总计))在蟒蛇3中_Python_Python 3.x - Fatal编程技术网

Python (“总计:$%.2f”(总计))在蟒蛇3中

Python (“总计:$%.2f”(总计))在蟒蛇3中,python,python-3.x,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")

我一直收到一个语法错误:以下打印语句的语法无效:

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))
采购数量:
每项价格:
是否要输入其他项目?
键入“c”继续,或键入“q”退出:
项目名称:
购买数量:
每项价格:
是否要输入其他项目?
键入“c”继续,或键入“q”退出:
项目名称:
购买数量:
每项价格:
是否要输入其他项目?
键入“c”继续,或键入“q”退出:
一杯牛奶@每杯2.99美元2.99美元
两个鸡蛋@每只3.99美元7.98美元
4个洋葱@每只0.79美元3.16美元
总计:14.13美元

正如注释中已经指出的那样,除了在最后一次调用
print
时缺少字符串格式运算符
%
之外,在最后一次调用
print
时还缺少右括号:

print(grocery_item[number], grocery_item['name'] % " @ $%.2f", grocery_item['price'], " ea $%.2f" % (item_total))

@Carcigenicate更可能遗漏的是字符串格式运算符
%
,而不是逗号。序言就是@blhsing所说的。您忘记了字符串后面的
%
运算符。它应该像
print(“总计:$%.2f”%(总计))
我两次都试过了,都没用。这是在Codio项目2中List@blhsing哦,你说得对。缺少逗号会导致类型错误,而不是SystaxError。