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

Python 如何通过添加物品的价格来获得总成本?,python,Python,这是我的代码,您也可以在这里运行它: 我想知道自行车=100、智能电视=150、汽车=150和iphone-X=300的价格。我的最后两行打印出价目表中的最后一个价格,即我输入的每件可用物品的300 你的问题不清楚,在编辑之前就清楚多了。但是,根据您的第一个解释,您的代码应该是: available_things=['bike','smart tv', 'car', 'i-phone X'] prices=[100,150,180,300] additionals=['smart phone'

这是我的代码,您也可以在这里运行它:


我想知道自行车=100、智能电视=150、汽车=150和iphone-X=300的价格。我的最后两行打印出价目表中的最后一个价格,即我输入的每件可用物品的300

你的问题不清楚,在编辑之前就清楚多了。但是,根据您的第一个解释,您的代码应该是:

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()

,那么问题出在哪里呢?输出是什么?没有其他提示吗?把代码扔给我们帮你解决?我不能在这里提供输出,因为它需要你输入。你可以查看我提供的链接中的输出。我的问题是,它会打印出我最后两行代码的价目表中的最后一个价格。我想获得bike=100、smart-tv=150、car=150和iphone-X=300的价格。你应该在问题中添加解释,而不是评论!
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)