如何在列表中将字符串连接在一起?

时间:2016-01-27 19:07:56

标签: python

我的任务如下

为在线食品服务编写菜单,可选择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")

1 个答案:

答案 0 :(得分:0)

要将项目添加到列表末尾,您需要使用

foodList.append(choice);

这样您就不必担心.insert()所涉及的索引编制。

这意味着您必须重新排列if语句或更好地了解insert()https://docs.python.org/2/tutorial/datastructures.html