2014-06-13 34 views
-4

我需要顯示這一點:迄今取得4個選擇= [ '1', '2', '8', '6']和成本=£20.8如何將列表轉換爲字符串,但顯示CSV文件的元素?

在這種格式:

盤無適合素食者

當然菜價

0起動煎明蝦4.5是

我ASLO需要嘗試,包括在年底很多菜都不怎麼適合素食者


這裏是我的嘗試:

spent = 0 
order = [] 
menu = [ 
    ['course', 'dish', 'price', 'suitable for vegetarians'], 
    ['starter', 'Liver Pate and Toast', '3.5', 'no'], 
    ['main course', 'Beef Wellington', '10.95', 'no'], 
    ['main course', 'Honey Glazed Duck Breast', '9.5', 'no'], 
    ['dessert', 'Tiramisu', '3.5', 'yes'], 
    ['dessert', 'Rhubarb Crumble', '3.9', 'yes'] 
] 

#Menu imported into python, no need to leave file open 
while True: 
    dishes = -1 

    for dish in menu: 
     if dishes == -1: 
       print ("Dish No".ljust(10), end="") 
     else: 
       print(str(dishes).ljust(10), end="") 

     print(dish[0].ljust(15), end="") 
     print(dish[1].ljust(30), end="") 
     print(dish[2].ljust(15), end="") 
     print(dish[3], end="\n\n") 
     dishes += 1 

    reply = input("Please choose your first item: ") 
    if reply.upper() == "Q": 
     break 

    print() 
    spent = spent + float(menu[int(reply)+1][2]) 
    order = order + [reply] 
    print(len(order), "choices made so far =", order, "and cost = £ ", spent) 
    print() 
    print ("Please choose an item from the menu (0-9 or press Q to end): ") 
    print() 
    print ((order)dish[0,1,2,3]) #Displays order as text and not list 

的CSV文件有這個裏面:適合素食者

當然菜價 啓動煎明蝦4.5是 起動山羊對Bruschetta的3.9奶酪是 開胃菜肝醬和烤麪包3.5否 主菜牛肉惠靈頓10.95否 主菜素食Filo肉醬7.5是 主菜水煮三文魚配蒔蘿11.5否 主菜蜂蜜釉面鴨胸9 0.5沒有 甜點提拉米蘇3.5是 甜點深盤蘋果餅3.9是 甜點大黃金寶3.9是

+0

有人曾提到在一個單獨的職位看str.join但我沒有想法如何使用它 – kungfool

+0

太棒了!現在,最後一行還有一個錯誤('SyntaxError:invalid syntax')。看看你是否可以解決你想做的事情。然後我可以運行它:)。 – Veedrac

+0

最後一行可以被刪除,這是我堅持的位。我正在嘗試獲取已被選擇打印爲文本列表而不是整數的訂單。 最後一行是我執行 – kungfool

回答

0

考慮

reply = input("Please choose your first item: ") 
    if reply.upper() == "Q": 
     break 

你想把它當作一個整數,所以做的第一:

reply = int(reply) 

添加到spentorder

print() 
    spent += float(menu[reply+1][2]) 
    order.append(reply) 

打印一些菜單的東西,之前:

print(len(order), "choices made so far =", order, "and cost = £ ", spent) 
    print() 
    print ("Please choose an item from the menu (0-9 or press Q to end): ") 
    print() 

您可以打印與menu[reply+1][1]選擇的項目:

print("You ordered", menu[reply+1][1], "bringing your total to", spent) 
    print() 
+0

這幾乎做到了。由於嘗試和除了有一個完全單獨的錯誤,但它現在大部分工作。感謝您的幫助,並保持耐心 – kungfool

相關問題