2015-10-11 31 views
1

我一直在這個小時,我似乎無法弄清楚如何返回總計。我所知道的是,我需要從總數= 0開始。我已經嘗試了'for x in'循環,我希望我讓他們在這裏發帖,但我一直在做試驗和錯誤,以至於我不能完全記得我所做的。我使用的Python 2.7.10,我是一個完整的初學者。請幫忙?即使只是一個提示?如何退貨購物車總計

f_list = [] 
print 'Enter a fruit name (or done):', 
f = raw_input() 
while f != 'done': 
    print 'Enter a fruit name (or done):', 
    f_list.append(f) 
    f = raw_input() 
print "" 
p_list = [] 
for i in f_list: 
    print 'Enter the price for ' + i + ':', 
    p = float(raw_input()) 
    p_list.append(p) 
print "" 
print 'Your fruit list is: ' + str(f_list) 
print 'Your price list is: ' + str(p_list) 
print "" 
n = len(f_list) 
r = range(0,n) 
q_list = [] 
for i in r: 
    print str(f_list[i]) + '(' + '$' + str(p_list[i]) + ')', 
    print 'Quantity:', 
    q = raw_input() 
total = 0 
+0

哪部分你不明白?您需要爲每種水果收集數量的水果並總結「數量*價格」。 –

回答

0

您忘記了創建數量列表,這不會有幫助。 然後只是遍歷你的f_list並添加它們。

f_list = [] 
print 'Enter a fruit name (or done):', 
f = raw_input() 
while f != 'done': 
    print 'Enter a fruit name (or done):', 
    f_list.append(f) 
    f = raw_input() 
print "" 
p_list = [] 
for i in f_list: 
    print 'Enter the price for ' + i + ':', 
    p = float(raw_input()) 
    p_list.append(p) 
print "" 
print 'Your fruit list is: ' + str(f_list) 
print 'Your price list is: ' + str(p_list) 
print "" 
q_list = [] 
for i in range(len(f_list)): 
    print str(f_list[i]) + '(' + '$' + str(p_list[i]) + ')', 
    print 'Quantity:', 
    q = raw_input() 
    q_list.append(q) 
total = 0 
for i in range(len(f_list)): 
    total += float(p_list[i]) * int(q_list[i]) 
print "Basket value : ${:.2f}".format(total) 
0

肯定有這個網站,應該可以幫助你其他的答案,但你想您的問與答轉換爲浮動和追加●至q_list就像對於p。要獲得總計,您需要做的只是total = sum(q_list)並顯示您的答案。