2 回答
TA贡献1853条经验 获得超6个赞
而不是extend在您的清单上,您实际上想要append该项目。extend将另一个列表连接到您的列表中,向列表中append添加一个值。
但实际上,我们根本不需要这个列表来做你想做的事情。相反,我们可以在考虑项目时将价格添加到总数中。我们也可以在这里打印价格,只依靠foundIt标志输出错误信息
# Declare variables.
NUM_ITEMS = 5 # Named constant
# Initialized list of add-ins
addIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]
# Initialized list of add-in prices
addInPrices = [.89, .25, .59, 1.50, 1.75]
# Flag variable
orderTotal = 2.00 # All orders start with a 2.00 charge
# Get user input
#
addIn = ""
addIn = input("Enter coffee add-in or XXX to quit: ")
# Write the rest of the program here.
while addIn != "XXX":
foundIt = False
for i in range(len(addIns)):
if addIn == addIns[i]:
print("Found match!")
orderTotal += addInPrices[i]
foundIt = True
print("{} Price is ${}".format(addIns[i],addInPrices[i]))
addIn = input("Enter coffee add-in or XXX to quit: ")
continue
print("Sorry, we do not carry that.")
addIn = input("Enter coffee add-in or XXX to quit: ")
print("Order Total is ${}".format(orderTotal))
TA贡献1826条经验 获得超6个赞
而不是 addcost 是可变的,它应该是列表。扩展运算符仅适用于列表。
# Declare variables.
NUM_ITEMS = 5 # Named constant
# Initialized list of add-ins
addIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]
# Initialized list of add-in prices
addInPrices = [.89, .25, .59, 1.50, 1.75]
# Flag variable
orderTotal = 2.00 # All orders start with a 2.00 charge
# Get user input
#
addIn = ""
addIn = input("Enter coffee add-in or XXX to quit: ")
# Write the rest of the program here.
while addIn != "XXX":
foundIt = False
for i in range(0, len(addInPrices)):
price = addInPrices[i]
product = addIns[i]
if addIn == product:
foundIt = True
break
if foundIt == True:
print("{} Price is ${}".format(product,price))
else:
print("Sorry, we do not carry that.")
addIn = input("Enter coffee add-in or XXX to quit: ")
# MY COMMENT --- Want to create new list from input above when foundIT == True and sum total to print out total order cost.
newList=[] #Create new list to grab values when foundIt == True
while foundIt == True:
addCost=[price]
newList.extend(addCost)
foundIt == True
break
else:
foundIt == False
print(newList)
print("Order Total is ${}".format(orderTotal+sum(addCost)))
添加回答
举报