Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

typing a phrase and printing a list at anytime in a function

in my function I want to be able to print the following list:

toppings = ["ONION, TOMATO, GREEN PEPPER, MUSHROOM, OLIVE, SPINACH"]

at any time just by typing "X" at any point in my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can just use the input() in a while True: loop to get the topping input or the X input.

toppings = ["ONION, TOMATO, GREEN PEPPER, MUSHROOM, OLIVE, SPINACH"] 
added_toppings = []

def chooseToppings(): 
    print("Type in one of our toppings and add it to your pizza. To see the full list of toppings, enter 'X'.") 
    while True:
        x = input()
        if x == "X":
            print(toppings)
        if x == "ONION":
            added_toppings.append("ONION")
        if x == "TOMATO":
            added_toppings.append("TOMATO")
        if x == "GREEN PEPPER":
            added_toppings.append("GREEN PEPPER")
        if x == "MUSHROOM":
            added_toppings.append("MUSHROOM")
        if x == "OLIVE":
            added_toppings.append("OLIVE")
        if x == "SPINACH":
            added_toppings.append("SPINACH")
        if x == "quit":
            break   
    print(added_toppings)

chooseToppings()

It checks every iteration for your input.

If input = "X" it prints the list of toppings.

If input = "TOMATO" it adds "TOMATO" to the added_toppings list.

If input = "quit" it breaks the loop and prints the added_toppings list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading