简体   繁体   中英

difficult: How to get whatever the user inputs in a list to be printed such that no element is left out?

I've spent hours trying to figure this out but kept getting errors and am clueless on how to proceed. Basically I am trying to make a bot that allows you to order one or two dishes in either a category of vietnamese dish or italian dish or both of the same kind. For the part I'm stuck on, I have to make an input function, such that the user types in whatever food they want in the form "["dish"] followed by typing in whatever price they want for that dish ["price"].


This is what the output screen is supposed to look like: (stuff typed in [] beside ==> is what the user types)

  • Input the lists exactly using this format ["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 
============================ 

displays this:

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

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

i1  - cadt
v2  - doh

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

i2  - dosh
v3  - dfd

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

when i use the following code

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

Change the second if statement in dish_codeslist to this:

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

EDIT:

    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")

EDIT 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

EDIT 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)
# 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)

this code may help to solve your problem, first give this a try by run it in a new python file

在此处输入图像描述

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)

Add this piece of code to your code and try running it to see whether it satisfies your requirements. The handling of user inputs whether they input the dish from any category gets added to a new list in this piece of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM