# Python return failure

**URL:** <https://discuss.python.org/t/python-return-failure/44520>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 30, 2024, 4:06am UTC](https://discuss.python.org/t/python-return-failure/44520 "2024-01-30T04:06:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 4:06am UTC](https://discuss.python.org/t/python-return-failure/44520/1 "2024-01-30T04:06:12Z")

</div>

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.

```python
#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)

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [January 30, 2024, 5:20am UTC](https://discuss.python.org/t/python-return-failure/44520/2 "2024-01-30T05:20:01Z")

</div>

> [@Lego12](#):
>
> The return True; wont return

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?

---

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 12:35pm UTC](https://discuss.python.org/t/python-return-failure/44520/3 "2024-01-30T12:35:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [January 30, 2024, 4:33pm UTC](https://discuss.python.org/t/python-return-failure/44520/4 "2024-01-30T16:33:16Z")

</div>

- 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:

```python
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

```python
if("MAI" in nput):
    return True
# missing return statement if "MAI" not in nput - implicitly returns None :/

```

by

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

```

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

```python
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](https://docs.python.org/3/library/pdb.html) - which seems difficult at first sight, but is actually pretty simple to use. You can find a decent tutorial [here](https://realpython.com/python-debugging-pdb/).

---

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 11:11pm UTC](https://discuss.python.org/t/python-return-failure/44520/5 "2024-01-30T23:11:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [January 30, 2024, 11:21pm UTC](https://discuss.python.org/t/python-return-failure/44520/6 "2024-01-30T23:21:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 11:22pm UTC](https://discuss.python.org/t/python-return-failure/44520/7 "2024-01-30T23:22:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 11:22pm UTC](https://discuss.python.org/t/python-return-failure/44520/8 "2024-01-30T23:22:37Z")

</div>

ill look a bit deeper, thank you

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [January 30, 2024, 11:22pm UTC](https://discuss.python.org/t/python-return-failure/44520/9 "2024-01-30T23:22:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lego12](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@Lego12](https://discuss.python.org/u/Lego12)\
**Post date:** [January 30, 2024, 11:41pm UTC](https://discuss.python.org/t/python-return-failure/44520/10 "2024-01-30T23:41:53Z")

</div>

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

```python
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

```
