2016-01-27 48 views
-5

我的任務是以下如何在列表中將字符串連接在一起?

寫菜單,用4個不同的美食選擇在線餐飲服務,即:墨西哥

每個菜餚都應該有至少5點不同的選擇,如咖喱然後咖喱汁然後熱咖喱等。它應該用訂單號打印出用戶的最終訂單。

我的問題是怎麼做我一起度過的順序,然後加入一些字符串列表,例如:加入熱水以蔬菜咖喱,但目前它把它放在不同的順序

最後使代碼高效我應該使用def函數嗎? 這是當前的代碼。 進口隨機

  print("Welcome to Hungry Horse...") 
      print("We have wide range of food choice from all around the world") 
      print("We have food choices from: Indian/Italian/Chinese/Mexican") 
      foodList = ([]) 

      drinkCho = input("Lets start with drinks. State if you would like drinks yes or no").lower() 
      if drinkCho == "yes": 
       drinkCho2 = input("Which drink do you want? We have option of:Coke/Fanta/Cobra/Water/FruitShoot").lower() 
       if drinkCho2 == "coke": 
          foodList.insert(1,drinkCho2) 
          print("You have chosen coke.A great choice") 
       if drinkCho2 == "fanta": 
          foodList.insert(2,drinkCho2) 
          print("You have chosen fanta.A great choice") 
       if drinkCho2 == "cobra": 
          foodList.insert(3,drinkCho2) 
          print("You have chosen cobra.A great choice") 
       if drinkCho2 == "Water": 
          foodList.insert(4,drinkCho2) 
          print("You have chosen Water.A great choice") 
       if drinkCho2 == "fruitshoot": 
          foodList.insert(5,drinkCho2) 
          print("You have chosen fruitshoot.A great choice") 

      elif drinkCho == 'No': 
       print("Ok no problem lets move on to the staters") 
      mainCho = input("Which type of mains would you like:Indian/Italian/Chinese/Mexican").lower() 
      if mainCho == "Indian": 
        print("Get ready to be enticed by the rich spicy falvors of Indian cusine") 
        indCho = input("What would you like to order: curry/onion bhaji/samosa") 
        if indCho == "curry": 
          foodList.insert(indCho) 
      curryCho = input("What curry would you like: chicken curry/lamb curry/ veg curry") 
      if curryCho == "veg curry": 
         print("Thats a excellent choice") 
         foodList.insert(6,curryCho) 
      elif curryCho == "lamb curry": 
         print("Thats a excellent choice") 
         foodList.insert(7,curryCho) 
      elif curryCho == "chicken curry": 
         foodList.insert(8,curryCho) 
         print("Thats a excellent choice") 
      else: 
       print("inncorrect input") 
       curryCho = input("What curry would you like: chicken curry/lamb curry/ veg curry") 
       foodList.insert(curryCho) 

      indSide = input(str("What do you want to eat the delicious with the curry naan bread/rice or both")) 
      if indSide == "naan bread": 
       foodList.insert(9,indSide) 
      elif indSide == "rice": 
       foodList.insert(10,indSide) 
      elif indSide == "both": 
       foodList.insert(11,indSide) 
      spiceAn = input("How spicy would you like your curry to be:hot/medium/mild").lower() 
      if spiceAn == "Hot": 
       print("ooh you must a man!") 
       foodList.insert(12,spiceAn) 
      elif spiceAn == "Medium": 
       print("That is a safe option") 
       foodList.insert(13,spiceAn) 
      elif spiceAn == "Mild": 
       ("You don't seen to like spicy food!") 
       foodList.insert(14,spiceAn) 

      rnd = (int(random.randrange(100)) + 1) 

      print ("Your final order", foodList ," Your order number is", rnd, "Please wait until we process your order") 
+0

也許你.. –

+1

這是不是「我們做你的功課」服務... – Stophface

+0

爲什麼你們不喜歡的職位,但沒有給出理由 –

回答

0

要將項目添加到列表的末尾,你要使用

foodList.append(choice); 

這樣你就不需要擔心參與.insert()索引。

這意味着你要麼必須重新安排你的if語句或得到更好的熟悉insert():可以有字典與美食的按鍵和可能添加的列表中爲每個作爲一種價值https://docs.python.org/2/tutorial/datastructures.html

+0

我的意思是如果veg咖喱是在列表中,也是熱添加到列表中,我怎麼把veg咖喱放在熱門的菜單上,所以它打印出熱蔬菜咖喱 –

+0

啊,我想你可能會誤解insert()函數。您輸入的數字是該項目放置在列表中的索引。你已經把自己的選擇列表中的每個項目插入不同的地方。例如,可樂和fanta應該在同一個索引中,但是看起來他們會選擇不同的索引 –

+0

我想明白我在說什麼 –

相關問題