Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 难点:如何让用户在列表中输入的内容打印出来,从而不遗漏任何元素?_Python_String_List - Fatal编程技术网

Python 难点:如何让用户在列表中输入的内容打印出来,从而不遗漏任何元素?

Python 难点:如何让用户在列表中输入的内容打印出来,从而不遗漏任何元素?,python,string,list,Python,String,List,我花了好几个小时试图弄明白这一点,但不断出现错误,不知道如何继续。 基本上,我正在尝试制作一个机器人,允许你点一两道菜,要么是越南菜,要么是意大利菜,要么两者都是同类菜。对于我一直坚持的部分,我必须做一个输入函数,这样用户就可以以“[“dish”]的形式输入他们想要的任何食物,然后输入他们想要的任何价格[“price”] 这就是输出屏幕应该是什么样子的:(在[]旁边键入的内容==>是用户键入的内容) 使用此格式[“dish”]准确输入列表 显示以下内容: =================

我花了好几个小时试图弄明白这一点,但不断出现错误,不知道如何继续。 基本上,我正在尝试制作一个机器人,允许你点一两道菜,要么是越南菜,要么是意大利菜,要么两者都是同类菜。对于我一直坚持的部分,我必须做一个输入函数,这样用户就可以以“[“dish”]的形式输入他们想要的任何食物,然后输入他们想要的任何价格[“price”]


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

  • 使用此格式[“dish”]准确输入列表
显示以下内容:

==================================
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

if(1dish\u 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
编辑:

if(1
编辑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
if(1


将这段代码添加到您的代码中,并尝试运行它,看看它是否满足您的要求。用户输入的处理,无论他们是否输入任何类别的菜肴,都会添加到这段代码的新列表中。

到目前为止,您有什么代码?您有可以与我们共享的代码吗?这有点混乱,我将继续发布是的,我快做完了,只是这一部分我真的不能完全理解如果(a==1)下面,我是Stuck,它是否适用于菜品,比如用户是否选择了3个、4个或6个类别,或者同时选择了这两个类别?如中所示,这将与他们输入的菜品一起打印一个代码?在这种情况下,如果你想让他们继续选择菜品,你需要使用
dict
while
循环例如,当你打印选择时,将每个食物项目存储在它们的键(即它们的代码)中。当他们选择带有代码的食物时,访问字典中的该代码。对于越南菜[i-1]如何使它计算列表中的每个元素。例如lista=(dog,cat,cow)所以v1=dog,v2=cat,v3=cow和listb=(蓝色,红色)i1=蓝色,i2=红色。所有这些,如何打印?这里是计算越南菜中的所有元素。对于意大利菜,您需要检查意大利菜列表的长度,并将其存储在变量中,比如说
b
,然后使用
if(1
并将所有
a
s更改为
b
。它工作了,但现在显示的显示列表如下所示:=============v1=item i1=item1 v2=item3。如何制作显示列表,以便==============分隔我显示的代码中所示的两组项目?值错误:int()的文本无效,以10为基数:'[2]“我输入了2道菜,第一道菜是价格[2],这是因为它不接受该格式中的值尝试按照您在图像中看到的方式进行格式化,使其与我提供的第一个代码中显示的相同,如=================================================================================================================================================================================================================================================
    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")
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
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)
# 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)
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)