Python Forum

Full Version: Trying to include an If statement in my While Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am trying to change some code from the following :
# pizza

def pizza():
    print("What toppings would you like on your pizza? All pizzas have cheese. ")
    print("Enter X when you have finished adding toppings. ")
    toppings = ""
    next_topping = ""
    while next_topping != "X":
        print("So far you have a pizza with cheese " + toppings)
        next_topping =  input("Enter topping ... ") 
        toppings = toppings + " and " + next_topping
        print("Add your next topping (X when finished)")
    print("Your pizza will have cheese " + toppings + ". Enjoy! ")

pizza()
to a code which removes the 'and X' if the user enters X to end the while loop. I have tried the following code but I am stuck on why this gives an error :

# pizza

def pizza():
    print("What toppings would you like on your pizza? All pizzas have cheese. ")
    print("Enter X when you have finished adding toppings. ")
    toppings = ""
    next_topping = ""
    while next_topping != "X":
        print("So far you have a pizza with cheese " + toppings)
        next_topping =  input("Enter topping ... ") 
        if next_topping = "X": 
          toppings = toppings
        else:
          toppings = toppings + " and " + next_topping[/b]
        print("Add your next topping (X when finished)")
    print("Your pizza will have cheese " + toppings + ". Enjoy! ")

pizza()
##My thinking here is if the user enters X then the toppings will just remain as toppings minus the X and if it doesnt include X then the toppings will be added as usual
(following uses f-string which requires python 3.6 or newer:
# pizza
 
def pizza():
    print("What toppings would you like on your pizza? All pizzas have cheese. ")
    print("Enter X when you have finished adding toppings. ")
    toppings = ""
    next_topping = ""
    while next_topping != "X":
        print(f'So far you have a pizza with cheese {toppings}')
        next_topping =  input("Enter topping ... ") 
        if next_topping.lower() == "x":
            break
        else:
            toppings = f'{toppings} and {next_topping}'
    print(f'Your pizza will have cheese {toppings}. Enjoy!')

pizza()
output:
Output:
What toppings would you like on your pizza? All pizzas have cheese. Enter X when you have finished adding toppings. So far you have a pizza with cheese Enter topping ... toads So far you have a pizza with cheese and toads Enter topping ... chicken feet So far you have a pizza with cheese and toads and chicken feet Enter topping ... orange marmalade So far you have a pizza with cheese and toads and chicken feet and orange marmalade Enter topping ... x Your pizza will have cheese and toads and chicken feet and orange marmalade. Enjoy!
Thanks for your code and help. Is there any way you can do this in Python 3 - without using the f string? (Edit) - Just figured it out :

# pizza
  
def pizza():
    print("What toppings would you like on your pizza? All pizzas have cheese. ")
    print("Enter X when you have finished adding toppings. ")
    toppings = ""
    next_topping = ""
    while next_topping != "X":
        print("So far you have a pizza with cheese "+ toppings)
        next_topping =  input("Enter topping ... ") 
        if next_topping == "X":
            break
        else:
            toppings = toppings + " and " + next_topping
    print("Your pizza will have cheese " + toppings + ". Enjoy! ")
 
pizza()
actually the better way is
print("Your pizza will have cheese {}. Enjoy! ".format(topping))
using concatenation (like you do) will fail if toppings is not str (e.g. it may be int and you will need an extra step to convert it to str)