简体   繁体   中英

Making a dictionary out of two lists taking 01 variable from 1st list and n (user defined) variable from the other

Please note that I am trying to make a program that take inputs of pizzas and toppings from user and make two lists (pizzas & Toppings) all using the user inputs. However I want to make a single dictionary using the two lists {element of first list: [values in pair (or n)]}.

Please find my program code below for your reference as well.

# List in Dictionary
# Making a list and dictionary with user inputs

p = int (input ("Enter the no. of pizzas you want to buy (max 3): "))
t = int (input ("Enter the toppings you would like in each pizza (max 4): "))
b = 1
pizzas = []
toppings = []
for pizza in range(p):
    pizza = ""
    pizza = str(input(f"\nEnter the flavor of Pizza No. {b}: "))
    pizzas.append (pizza)
    b += 1
    c = 1
    for topping in range(t):    
        topping = str(input(f"Enter the flavor of Topping No. {c}: "))
        toppings.append (topping)
        c += 1
print ()
print (pizzas)
print (toppings)

CURRENT OUTPUT

Enter the no. of pizzas you want to buy (max 3): 2
Enter the toppings you would like in each pizza (max 4): 2

Enter the flavor of Pizza No. 1: Chicken Tikka
Enter the flavor of Topping No. 1: Cheese
Enter the flavor of Topping No. 2: Tikka

Enter the flavor of Pizza No. 2: Veggie Pizza
Enter the flavor of Topping No. 1: Olives
Enter the flavor of Topping No. 2: Mushrooms

['Chicken Tikka', 'Veggie Pizza']
['Cheese', 'Tikka', 'Olives', 'Mushrooms']

REQUIRED OUTPUT

Enter the no. of pizzas you want to buy (max 3): 2
Enter the toppings you would like in each pizza (max 4): 2

Enter the flavor of Pizza No. 1: Chicken Tikka
Enter the flavor of Topping No. 1: Cheese
Enter the flavor of Topping No. 2: Tikka

Enter the flavor of Pizza No. 2: Veggie Pizza
Enter the flavor of Topping No. 1: Olives
Enter the flavor of Topping No. 2: Mushrooms

['Chicken Tikka', 'Veggie Pizza']
['Cheese', 'Tikka', 'Olives', 'Mushrooms']

Dict_pizza = {'Chicken Tikka':['Cheese','Tikka'], 'Veggie Pizza':['Olives', 'Mushrooms']} 

Tried equating the dict_pizza with input variables (didn't work) and also tried dict.fromkeys (didn't work as well) and my output comes out to be

Dict_pizza = {'Veggie Pizza':['Cheese','Tikka','Olives', 'Mushrooms']}

Final Note: Just started learning python 03 weeks back so kinda newbie to this language and this forum. Thank you in advance for your support and help.

You can make a dict with a list for for toppings.

dict_pizza = {}
for _ in range(p):
    pizza = ""
    pizza = str(input(f"\nEnter the flavor of Pizza No. {b}: "))
    dict_pizza[pizza] = []
    b += 1
    c = 1
    for _ in range(t):    
        topping = str(input(f"Enter the flavor of Topping No. {c}: "))
        dict_pizza[pizza].append(topping)
        c += 1

Then, dict_pizza will have the output as you said. The name of the pizzas only will be in dict_pizza.keys() and the topping of each specific pizza can be accessed by dict_pizza["pizza name"] .

I would clean your code a little more though.

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