Python return failure

Working on some homework, and for the past few hours I havent figured out how to fix this issue, ive tried doing many different things to it and doing much research, even tried an online IDE and it had the exact same issue… The return True; wont return, ive tried printing out right before the return and it prints fine. Thank you for reading.

#imports
import math
#functions
times_looped=0
def menus(menu_type):
  times_looped=0
  if(menu_type==0):
    print("Main Menu:")
  elif(menu_type==1):
    print("Drinks:")
  elif(menu_type==2):
    print("Fries:")
  #----------
  for i in menu[menu_type][0]:
    indention=""
    for j in range(20-len(i)):
      indention=indention+" ";#sets correct ammount of spaces to keep the prices looking linear.
    print(i+indention+str(menu[menu_type][2][times_looped]))#uses previous information given to make the next line on the menu.
    
    times_looped+=1
  times_looped=0
  if(menu_type==0):
    print("We also sell drinks and fries!")
  else:
    print("We also have a main menu!")

def nofc(food, food_type):#number_of_food_count() was a bit lengthy, so I shortened it.
  print("How much " + food.lower() + " would you like?")
  nput = input(">>>")
  if(nput.isdigit() and int(nput)>0):
    for i in range(int(nput)):
      menu[food_type][1].append(food.lower().capitalize())
    print("Successfully added " + nput +" "+ food +("s" if int(nput)>1  else "")+ " to your order.")
  elif(nput.count(".")==1 and ((str(i) in nput) for i in range(9))):
    if(float(nput)>0):
      print("I am going to pretend I didnt hear that.")#cuz le chickens are living
    else:
      print("Please only input whole numbers.")
  else:
    print("Please only input whole numbers.")

def order():
  nput = input(">>>").upper()#var name = funny way to spell input
  if("CHIC" in nput):
    nofc("Chicken", 0)
  elif("BEEF" in nput or "COW" in nput):
    nofc("Beef", 0)
    
  elif("VEG" in nput or "PLANT" in nput):
    nofc("Vegetarian", 0)
    
  elif("PORK" in nput or "PIG" in nput):
    nofc("Pork", 0)
    
  else:
    if("DRI" in nput):
      menus(1)
      nput = input(">>>").upper()
      for i in menu[1][1]:
        if (i.upper()==nput):
          nofc(nput.lower().capitalize(), 1)
          break
    elif("FRI" in nput or "FRY" in nput):
      if(nput=="FRY"):
        print("you want just one fry? how weird... oh well... guess you can have one... I will give it to you for free just to make it easier on me...")
        print("")
        print("\033[32mYou receive exactly one fry\033[0m")
      else:
        menus(2)
        nput = input(">>>").upper()
        for i in menu[2][1]:
          if (i.upper()==nput):
            nofc(nput.lower().capitalize(), 2)
            break
    elif(not "MAI" in nput):
      print("That is not an option, please try something else.")
  if("MAI" in nput):
    return True
    

#variables

global cheat_code
cheat_code="xyzzy"#might make a reference easter egg to the first text based video game.
global part
part=0#method for location
global x
x=False

#I know we are not alloud to do lists, but I got tired and I dont mind loosing a few points for that.
menu=[[],[],[]]
menu[0]=[#main
    ["Chicken","Beef","Vegetarian","Pork"],#-----#names
    [], #inventory-------------------------------#inventory
    [25.99, 50.99, 666.66, 70.19] #prices--------#prices
  ]
menu[1]=[#drinks
    ["Small", "Medium", "Large", "Extra Large"], #names
    [], #inventory-------------------------------#inventory
    [20.00, 40.00, 80.00, 160.00] #prices--------#prices
  ]
menu[2]=[#fries
    ["Small", "Medium", "Large", "Extra Large"], #names
    [], #inventory-------------------------------#inventory
    [160.00, 80.00, 40.00, 20.00] #prices--------#prices
  ]

print("welcome too hell, what would you like to order?")
print("we also sell expensive pills")
print("")
menus(0)
order()
print("Is that all?")
part=0

while True:
  nput = input(">>>").upper()#var name = funny way to spell input
  if(part==0 or part==0.1):
    if(("Y" in nput and not "NO" in nput) or "YES" in nput):
      part=1
    elif("N" in nput):
      menus(0)
      if(order()==True):
        print("")
      else:
        print("Is that all?")
        
    else:
      print("I am sorry, but I do not know the British language, I will just assume that is a yes.")
      part=1
  if(part==1):
    print("part==1")
    print(menu)

Which return True are you talking about, and exactly what do you mean that it “won’t return”? According to your analysis, exactly what happens instead, and why is that wrong? In other words, what is happening that leads you to the conclusion that it “won’t return”?

It would certainly help to be walked through an example. What do you type as input to the program; what do you expect to happen as a result; what happens instead?

2 Likes

I expect to input Fries, then Main, and for “” to be printed.

  • You gave this post the title “Python return failure”. That is based on your struggle with to get this script working correctly. But that title is actually based on an incorrect analysis of what is going wrong.
  • You wrote that you tried the same code also in an online IDE and got the same unexpected results.
    Running the same code on a different host can sometimes help in debugging, but in pure Python code (as here) this usually doesn’t give you new hints.
  • In your script you have a mixture of global variables, like menu, x, part, nput, some functions, and a global control loop while True: ...
    Mixed in with that are a ton of input() calls and print statements. This makes the script very hard to read and debug. (Think of all the changes you’d have to make if your next surprise assignment was to translate all questions and answers into a different language.)
    This can be improved a lot by getting rid of all global variables and putting everything starting at print("Welcome...") ... while True: ... into a separate function (call it main or so).
  • You are writing in a few places if not X in Y. Python happens to parse this as you intended, but it’s better to write that as if X not in Y (which is really what you intend).
  • The easiest way to debug what actually is going wrong is often to add a few print statements at critical places. But since you already have so many prints, I would advise to make a little utility function at the top of your script:
DEBUG = False

def echo(*args):
      if DEBUG:
          print(*args)

By setting global variable DEBUG to True, you can activate this echo function. (The global variable is more or less justified in this case.)

  • Then you want echo calls, immediately after each of the if .. elif... clauses in your while True loop. Because that’s where your problem is located. The echo should just print out what the current variables are that are used in the clause. Also you do not want to print an empty line (print("")) but change this into something clearer like print("DONE!").
  • The order function either returns True or None - This can be improved by replacing the last few lines
if("MAI" in nput):
    return True
# missing return statement if "MAI" not in nput - implicitly returns None :/

by

return "MAI" in nput  # clearly returns either True or False

Before returning there, I would also add an echo call, something like this:

echo("input was ", nput, " -> returning ", "MAI" in nput)
return "MAI" in nput
  • Adding those extra echo print statements is a poor-person’s way of debugging, but it’s often the easiest and fastest way to find out where a problem is coming from. If you really want to dive into what a program is doing and inspect all details, then you could use pdb - which seems difficult at first sight, but is actually pretty simple to use. You can find a decent tutorial here.
1 Like

Thank you, thanks to you, I have learned to keep my code more compact and efficient. I dont know if this will help with the bug but I will certainly apply this to what I already have and it may fix it, if not, makes it easier to read. thank you!

It should fix it. I did find where your actual bug is - but I thought it was more important to give you a method to discover it yourself - I trust you will be able to now.

1 Like

I change the return and it still seems to ignore the return True. btw I already checked the line by putting a print("TEST") before the return statement and it worked fine.

ill look a bit deeper, thank you

Yes - the order function by itself is OK. But make sure to also use the echo statements to look at what happens inside the while loop. (If you really cannot find it, then please post later how you changed the while loop, so someone could perhaps give more hints.)

1 Like

lmao, found the bug, tysm. You taught me how to debug efficiently, and so much more, tysm.

print("welcome too hell, what would you like to order?")
print("we also sell expensive pills")
print("")
menus(0)
order()
print("Is that all?")
part=0
1 Like