【发布时间】:2021-03-20 02:21:46
【问题描述】:
我对编程很陌生。
我的问题是我的 if 语句出现了 IndexError。因此,我将 if 语句放入另一个单元格并使用不同的列表进行了尝试,它工作正常。我做错了什么?
from datetime import datetime
import math
def GroceryReceipt():
grocery = [["bread", 3], ["eggs", 3], ["soda", 7], ["chips", 3], ["beef", 8]]
amountGroceries = int(input("Input the amount of food you wish to buy: "))
userGroc = []
for i in range(amountGroceries):
buyGroceries = input("Pick some foods: ")
userGroc.append(buyGroceries)
for j in range(len(grocery)):
if(len(userGroc) < j):
break
else:
print("$",grocery[j][1])
if(userGroc[j] in grocery[j][0]):
add = grocery[j][1] + grocery[j][1]
tax = math.pi/100 * add
total = tax + add
round(total, 2)
x = datetime.today()
print(total, x)
GroceryReceipt()
【问题讨论】:
-
请根据用户的相应输入分享一个您期望的输出示例。很难用
for j循环来判断您要完成什么。 -
我正在尝试获取杂货清单中项目旁边的整数,即其成本。然后我想用进口数学加总和加税,还想打印购买的日期和时间。
标签: python-3.x function