如何通过增加物品价格来获得总成本?

时间:2017-12-15 13:53:02

标签: python

以下是我的代码,您也可以在此处运行:https://repl.it/repls/MonumentalViciousMamba

available_things=["bike","smart tv","car","i-phone X"]
prices=[100,150,180,300] 
additionals=["smart phone","xbox","ps4","laptop","chromebook"]

things=raw_input("\nWhat you wish to get:")
selected_additionals=[]
for val in range(0,3):
    selected_additionals.append(raw_input("Note: You will be charged $20 for each additional item. Please enter an additional item:"))

for selected_additional in selected_additionals: 
    if selected_additional not in additionals and selected_additional != "":
        print("\nSorry we don't have "+str(selected_additional)+"\nBut we will change it to a gift. Note the gift also charges $20") 

if size in availablePizzas:
    print("Your total cost is: $"+str(pizzaPrice+2)) 

我想得到自行车的价格= 100,智能电视= 150,汽车= 150,以及iphone-X = 300。我的最后两行打印出价格列表中的最后一个价格,即我输入的每个可用商品的价格为300。

1 个答案:

答案 0 :(得分:0)

你的问题不明确,编辑前要清楚得多。但是,根据您的第一个解释,您的代码应该是:

available_things=['bike','smart tv', 'car', 'i-phone X']
prices=[100,150,180,300] 
additionals=['smart phone', 'xbox', 'ps4' , 'laptop', 'chromebook']

selected_additionals= [str(i) for i in raw_input("What you wish to get:").split()] # making a list of items which user inserts
#selected_additionals = ['bike', 'X', 'car'] # here I expect $100 + $180 + $20 = $300 as output

Totalprice = []
for i in selected_additionals: 
    if i in available_things: # comparing 2 lists
        Totalprice.append(prices[available_things.index(i)])
    else:
        Totalprice.append(20) #charge $20 on each item which is not in the avaialable_things

print sum(Totalprice)

我假设您使用的是Python 2,对于python 3,您应该使用input()