Trying to practice python atm and have a looping code, however something with either the loop or if; is broken, and im not sure what. to my knowledge this code should work however on VSC it is not outputting correctly, constantly giving line 18 {print(“Thank you for your order, your pizza of “, ingredients, “is being prepared”)} and restarting regardless of if restart = false.
Any ideas fellas?
restart = True # variable to make it restart
def start() : #start pos to return to
#Pizza time!
ingredients = ['Mozzerala', 'basil', 'tomato', 'garlic', 'olive oil']
print(ingredients)
#above is the base pizza, below is where you add extra ingredients
extra = (input("input extra ingredients here;"))
print(ingredients, "with addional", extra)
ingredients.append(extra)
print(ingredients)
rem = (input("input undesired ingredients here;"))
print(ingredients, "without", rem)
ingredients.remove(rem)
print(ingredients)
final = input('is this correct?') #confirmation check
if final == ('yes') or ('y') or ("confirm") or ("Yes"):
restart = False
print("Thank you for your order, your pizza of ", ingredients, "is being prepared") #will no longer loop as restart is false
elif final == ("no") or ("No") or ("Wrong"):
print("Sorry, Restarting order")
else:
print('Restarting order')
while restart == True: # this will loop until restart is set to be False
start()
Bullet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
The restart
variable that you assign to within start()
is a local variable to that function, and is not the same as the file-level restart
that you assign to at the first line, and check in your while
condition.
To prevent this, your function start
could return True
or False
to indicate whether you want the loop to continue. Then, you could replace your last lines with:
while restart == True:
restart = start()
Using the break
keyword (and moving the code from start()
directly into the while
loop) is another way to control this.