2017-04-21 7 views
0

各コストに対応する販売数を掛けることができるようにしようとしています。たとえば、1.99 * 10,1.49 * 5などとなります。また、リスト内のコストに応じて、最も高価な製品または最も安価な製品の製品名をどのように印刷するかを理解できないようです。私はproduct_costのiをproduct_soldの対応するiと掛け合わせようとしましたが、その答えは途方もないと思われます。誰にどのようにこれを修正するための任意のアイデアを持っていますか?おかげ2つのリスト内の値を掛け合わせてpythonで印刷する

ただし、以下のコードで、

# product lists 
product_names = ["prime numbers", "multiplication tables", "mortgage calculator"] 
product_costs = [1.99, 1.49, 2.49] 
product_sold = [10, 5, 15] 

def report_product(): 
    total = 0 
    print("Most expensive product:", max(product_costs)) 
    print("Least expensive product:", min(product_costs)) 
    for i in range(len(product_costs)): 
     total += i * product_sold[i] 
    print("Total value of all products:", total) 

selection = "" 

while selection != "q": 
    selection = input("(s)earch, (l)ist, (a)dd, (r)emove, (u)pdate, r(e)port or (q)uit: ") 

    if selection == 'q': 
     break 
    elif selection == 's': 
     search_product() 
    elif selection == "l": 
     list_products() 
    elif selection == "a": 
     add_products() 
    elif selection == "r": 
     remove_products() 
    elif selection == "u": 
     update_products() 
    elif selection == "e": 
     report_product() 
    else: 
     print("Invalid option, try again") 

print("Thanks for looking at my programs!") 
+0

!whileループの条件が選択しながら、'であるので、break'が冗長である=「ループはとにかく – abccd

+0

を破る作るq'' uはsearch_productのコードを表示することができ、 list_products、remove_products、update_products、report_product関数.. .. ?? – shiva

答えて

0

は、対応するインデックスの新しい配列が

、最も高価だった製品の名前を見つけるには、

product_costs = [1.99, 1.49, 2.49] 
product_sold = [10, 5, 15] 
product_mult = list(map(lambda x,y: x*y, product_costs,product_sold)) 

を掛けますするには

index = product_costs.index(max(product_costs)) 
most_expensive = product_names[index] 
+1

'[product_sold内のyのproduct_costs内のxのx *のx * y] 'は、[19.9,9.95,29.85,14.9,7.45,22.35,24.900000000000002,12.450000000000001,37.35]の3つのOPではありません。を探している。 – cdlane

+0

ありがとうございました! – Windmill

+0

'index = product_mult.index(max(product_mult))'というコードは、*もっとも*高価な*製品ではなく、もっとも*得られた製品を見つけ出します。 – cdlane

1

必ずしも最適ではありませんが、最小限のコードで回答を得ることができますusi zip() NG:

def report_product(): 

    print('Most expensive product:', max(zip(product_costs, product_names))[1]) 

    print('Least expensive product:', min(zip(product_costs, product_names))[1]) 

    total_earned = sum(cost * sold for cost, sold in zip(product_costs, product_sold)) 

    print('Total earned from all products sold: ${:.2f}'.format(total_earned)) 

OUTPUT

Most expensive product: mortgage calculator 
Least expensive product: multiplication tables 
Total earned from all products sold: $64.70 
0
>>> prod_m = [a*b for a,b in zip(product_costs, product_sold)] 
>>> prod_m 
[19.9, 7.45, 37.35] 
>>> product_names[product_costs.index(max(product_costs))] 
'mortgage calculator' 
>>> product_names[product_costs.index(min(product_costs))] 
'multiplication tables' 

また、二つのリストを掛けるためにnumpyの使用することができます。 `選択の場合==「Q」::トピックオフ

>>> import numpy 
>>> list(numpy.array(product_costs)*numpy.array(product_sold)) 
[19.899999999999999, 7.4500000000000002, 37.350000000000001] 
関連する問題