难点:如何获得用户在列表中输入的任何内容,以便打印时不遗漏任何元素?

2024-05-08 09:03:15 发布

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

我花了几个小时试图弄清楚这个问题,但不断出现错误,我不知道如何继续。 基本上,我正在尝试做一个机器人,让你可以在一个越南菜或意大利菜或同类的一个或两个类别的菜。对于我一直坚持的部分,我必须做一个输入函数,这样用户就可以以“[”dish“]的形式键入他们想要的任何食物,然后键入他们想要的任何菜价[”price“]。你知道吗


输出屏幕应该是这样的:(在[]中键入的内容旁边==>;是用户键入的内容)

  • 使用以下格式输入列表[“dish”]

Lists should have at least 1 dish and not more than 10 dishes 

Lists with prices correspond exactly to lists with dishes 

Execute with new lists (n) or original lists (o)? ==> n 

List of Vietnamese dishes ==> ["water", "rice", "pancake", "steamed sticky rice"] 
List of Vietnamese dishes prices ==> [7.5, 6.75, 5.15, 8.25] 
List of Italian dishes ==> ["pizza", "meatball spaghetti", "pasta"] 
List of Italian dishes prices ==> [7.15, 6.25, 5.0] 
*** TRACE Vietnamese  ['Pho', 'Fried rice', 'Pancake', 'Steamed sticky rice'] [7.5, 6.75, 5.15, 8.25] 
*** TRACE Italian  ['Pizza', 'Meatball spaghetti', 'Pasta'] [7.15, 6.25, 5.0] 


Order a dish? y/n ==> (y) 
All the available dishes are 
============================ 
v1 ‐ Pho 
v2 ‐ Fried rice 
v3 ‐ Pancake 
v4 ‐ Steamed sticky rice 
============================ 
i1 ‐ Pizza 
i2 ‐ Meatball spaghetti 
i3 ‐ Pasta 
============================ 

显示以下内容:

==================================
v1  - cat

==================================

i1  - cadt
v2  - doh

==================================

i2  - dosh
v3  - dfd

==================================

当我使用以下代码时

if (1 < a < 10 and 1 < b <10):
        for i in range(1,a+1) and range(1,b+1):
            print("v"+str(i)," -", vietnamese_dishes[i-1])
            print("\n==================================\n")
            print("i"+str(i)," -", italian_dishes[i-1])
        print("\n\nPlease choose another dish by indicating the code that we provide")
        print("\nYou may order the same dish as before, if you want")
        print("\nIf you do not choose an existing dish we will choose one for you")
        return

Tags: andof键入withlistslistpricesprint
3条回答

dish_codeslist中的第二个if语句更改为:

if (1 < a < 10):
        for i in range(1,a+1):
            print("v"+str(i)," -", vietnamese_dishes[i-1])
        print("\n\nPlease choose another dish by indicating the code that we provide")
        print("\nYou may order the same dish as before, if you want")
        print("\nIf you do not choose an existing dish we will choose one for you")
        return

编辑:

    if (1 < a < 10):
        for i in range(1,a+1):
            print("v"+str(i)," -", vietnamese_dishes[i-1])
    print("\n==================================")
    if (1 < b < 10):
        for i in range(1,b+1):
            print("i"+str(i)," -", italian_dishes[i-1])
        print("\n\nPlease choose another dish by indicating the code that we provide")
        print("\nYou may order the same dish as before, if you want")
        print("\nIf you do not choose an existing dish we will choose one for you")

编辑2:

if (1 < a < 10 and 1 < b <10):
        for i in range(1,a+1):
            print("v"+str(i)," -", vietnamese_dishes[i-1])
        print("\n==================================\n")
        for i in range(1,b+1):
            print("i"+str(i)," -", italian_dishes[i-1])
        print("\n==================================\n")
        print("\n\nPlease choose another dish by indicating the code that we provide")
        print("\nYou may order the same dish as before, if you want")
        print("\nIf you do not choose an existing dish we will choose one for you")
        return

编辑3:

def dish_codeslist():
    a = len(vietnamese_dishes)
    b = len(italian_dishes)
    dish_dict = {}
    print("\n\nAll the available dishes are")
    print("\n==================================")
    if (a == 1):
        print("v"+str(a)," -", vietnamese_dishes[0])
        print("\n\nPlease choose another dish by indicating the code that we provide")
        print("\nYou may order the same dish as before, if you want")
        print("\nIf you do not choose an existing dish we will choose one for you")
        return
    if (1 < a < 10 and 1 < b <10):
        for i in range(1,a+1):
            print("v"+str(i)," -", vietnamese_dishes[i-1])
            dish_dict["v"+str(i)] = (vietnamese_dishes[i-1],vietnamese_dish_prices[i-1])
        print("\n==================================\n")
        for i in range(1,b+1):
            print("i"+str(i)," -", italian_dishes[i-1])
            dish_dict["i"+str(i)] = (italian_dishes[i-1],italian_dish_prices[i-1])
        print("\n==================================\n")
        return dish_dict
def choose_dish(dish_dict):
    print("\n\nPlease choose another dish by indicating the code that we provide")
    print("\nYou may order the same dish as before, if you want")
    print("\nIf you do not choose an existing dish we will choose one for you")
    choice = input("Provide the dish code here (MUST BE a letter and a number) ==> ")
    if choice in dish_dict.keys():
        print(f"*** TRACE: dish {dish_dict[choice][0]} price {dish_dict[choice][1]} ")
    else:
        print("You did not enter a valid choice, we will suggest a dish for you")
        import random
        dish = random.choice([*dish_dict.values()])
        print("*** TRACE: dish {dish[0]} price {dish[1]} ")
#### Start
codes_setup()
dish_dict = dish_codeslist()
choose_dish(dish_dict)
def user_input(user_list):
    print("Enter the dish that you want to order")
    user_value = input()
    user_list.append(user_value)
    print(user_list)
    print("Do u want to add more dishes: ")
    answer = input()
    if answer == "Yes":
        user_input(user_list)
        return user_list
    else:
        return user_list
#### Start
codes_setup()
dish_codeslist()
user_list=[]
modified_list = user_input(user_list)
print("The modified new list is :")
print(modified_list)

将这段代码添加到您的代码中,并尝试运行它以查看它是否满足您的需求。在这段代码中,用户输入的处理(无论他们是否从任何类别输入菜)将被添加到一个新的列表中。你知道吗

# enter vietnames names dishes names here
vietnamese_dishes  = [input('enter the dish name: ') for i in range(int(input('how many dishes you want to put in vietnamese_dishes: ')))]

# enter the prices for corresponding vietnames dishes here 
vietnamese_dish_prices  = [int(input('enter the price for '+dish+ ': '))  for dish in vietnamese_dishes]

print(vietnamese_dishes)
print(vietnamese_dish_prices)

vietnames = [vietnamese_dishes, vietnamese_dish_prices]
print(vietnames)

此代码可能有助于解决您的问题,请首先尝试在新的python文件中运行它

enter image description here

相关问题 更多 >