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

2024-10-02 08:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务如下

写一个菜单,为一个在线食品服务与选项4种不同的菜肴,即:墨西哥菜

每道菜至少要有5种不同的选择,比如咖喱、咖喱酱、热咖喱等等。它应该打印出带有订单号的用户最终订单。你知道吗

我的问题是,如何将顺序组合在一起,然后将列表中的一些字符串连接起来,例如:add hot to veg curry,但目前它的顺序不同

最后,为了提高代码的效率,我应该使用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")

Tags: toyouinputifhavelikeinsertprint
1条回答
网友
1楼 · 发布于 2024-10-02 08:25:48

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

foodList.append(choice);

这样您就不必担心.insert()所涉及的索引。你知道吗

这意味着您要么重新排列if语句,要么更好地熟悉insert()https://docs.python.org/2/tutorial/datastructures.html

相关问题 更多 >

    热门问题