Python Forum
Trying to include an If statement in my While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to include an If statement in my While Loop
#1
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
Reply
#2
(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!
Reply
#3
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()
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to include one script into another? MorningWave 8 305 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  how include a python code in notpad++ plugin akbarza 2 580 Sep-25-2023, 08:25 PM
Last Post: deanhystad
Photo Python code: While loop with if statement HAMOUDA 1 535 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Regex Include and Exclude patterns in Same Expression starzar 2 736 May-23-2023, 09:12 AM
Last Post: Gribouillis
  Multiply and Addition in the same loop statement with logic. joelraj 2 999 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  How to include input as part of variable name Mark17 4 2,444 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,764 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,887 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,506 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Can I include text using artist? tetrisbot 0 1,408 Aug-13-2020, 08:13 PM
Last Post: tetrisbot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020