Bullet
Bullet

Reputation: 7

Loop broken, If statement/while

I am trying to practice Python at the moment and have a looping code, however something with either the loop or if; is broken, and I'm 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?

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()

Upvotes: -1

Views: 89

Answers (4)

Glauco
Glauco

Reputation: 1463

Also another error here, the final condition is wrongly handled

final == ('yes') or ('y') or ("confirm") or ("Yes")

should be:

final in ('yes','y', "confirm","Yes")

the right part, written in this way is evaluated at once. Here there are mode details

Upvotes: 0

C0untV
C0untV

Reputation: 59

##set your restart variable as a bool datatype so it can exist as True or False
restart = bool

def main():
    start()

def start(): #start pos to return to
    #Pizza time!
    ingredients = ['Mozzerrala', '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 additional", 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
    while restart == True:
        if final == ('yes') or ('y') or ("confirm") or ("Yes"):
            print("Thank you for your order, your pizza of ", ingredients, "is being prepared") #will no longer loop as restart is false
            break
        
        elif final == ("no") or ("No") or ("Wrong"):
            print("Sorry, Restarting order")
        else: 
            print('Restarting order')

if __name__ == "__main__":
    main()

You need to set your restart variable outside of your function so it exists where any function can access it- you can use global variables but you should avoid as it is a bad habit and can cause you issues down the line with more complex programs.

Be careful with your indents (although it might just be a pasting code thing which is fine) one thing to be aware of is your code breaks if someone inputs something in rem which isn't in the list of ingredients- but I imagine as you are learning you haven't covered this yet so you can ignore for now.

You can use the break keyword to get yourself out of a loop, so you create the while loop which will iterate over itself repeatedly until someone gives it a method to get out of the loop, which you deliver with break

Sorry if my explanation is confusing, feel free to ask any questions!

Upvotes: -1

Charlie Yan
Charlie Yan

Reputation: 11

Add global restart to your start function:

def start():
  global restart
  ...

Otherwise, you are creating a new local variable named restart inside start instead of using the global one.

Upvotes: 0

Dan
Dan

Reputation: 10786

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.

Upvotes: 1

Related Questions