# Help! I get stuck at this code ( don't know why is\_sufficient = False not working

**URL:** https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096
**Category:** discuss
**Tags:** ipython, how-to, help-wanted
**Created:** [December 26, 2023, 12:47am UTC](https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096 "2023-12-26T00:47:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![12N1-36-Lam\_M\_Van](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/12n1-36-lam_m_van/32/10854_2.png) [@12N1-36-Lam\_M\_Van](https://discourse.jupyter.org/u/12N1-36-Lam_M_Van)
#### Post date: [December 26, 2023, 12:47am UTC](https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096/1 "2023-12-26T00:47:20Z")

</div>

Hello, can someone help me find out how to stop making it print out the transaction function when there is not enough ingredient to make the order. (I just started Python last week so my code has a lot of bugs. I also attach the description file of the project. Thanks  
MENU = {  
“espresso”: {  
“ingredients”: {  
“water”: 50,  
“coffee”: 18,  
},  
“cost”: 1.5,  
},  
“latte”: {  
“ingredients”: {  
“water”: 200,  
“milk”: 150,  
“coffee”: 24,  
},  
“cost”: 2.5,  
},  
“cappuccino”: {  
“ingredients”: {  
“water”: 250,  
“milk”: 100,  
“coffee”: 24,  
},  
“cost”: 3.0,  
}  
}

resources = {  
“water”: 300,  
“milk”: 200,  
“coffee”: 100,  
}

is\_on= True  
money = 0  
#is\_sufficient = True  
is\_successful = True  
while is\_on:  
order =input(“What would you like?”)  
is\_sufficient = True  
if order ==“report”:  
print(resources)  
print(f"Money: {money}“)  
def is\_sufficient(order):  
for ingredient in resources:  
if resources[ingredient] \< MENU[order][“ingredients”][ingredient]:  
print(f"There is not enough {ingredient} to make {order}”)  
return False  
else:  
return True

is\_sufficient(order)  
if is\_sufficient:  
for ingredient in resources:  
resources[ingredient] -=MENU[order][“ingredients”][ingredient]  
print(resources[ingredient])

print(“Please insert coins”)  
quarters = int(input("Quarters: "))  
dimes = int(input("Dimes: "))  
nickles = int(input("Nickles: "))  
pennies = int(input("Pennies: “))  
def transaction(order,quarters,dimes,nickles,pennies):  
print(“Please insert coins”)  
total =quarters_0.25+dimes_0.1+ nickles_0.05 +pennies_0.01  
cost =MENU[order][“cost”]  
if total \>=cost:  
change = total - cost  
print(f"Here is your change ${change}”)  
return money +cost  
else:  
print(“Sorry that’s not enough money. Money refunded”)  
is\_successful = False  
return is\_successful  
if is\_sufficient:  
transaction(order,quarters,dimes,nickles,pennies)  
if is\_successful and is\_sufficient:  
print(“Here is your drink. Enjoy”)

---

<div class="post-metadata">

### Author: ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)
#### Post date: [December 26, 2023, 2:39pm UTC](https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096/2 "2023-12-26T14:39:54Z")

</div>

First, your code is unreadable and so anyone here who knows Python would be hard pressed to help you.  
Second, Jupyter is not just about Python. This forum is for issues caused by Jupyter and not for issues caused by languages that can be run in the language kernels that Jupyter can use.

Please read and follow [Getting good answers to your questions](https://discourse.jupyter.org/t/getting-good-answers-to-your-questions/1825) in future posts here.

* * *

More details on those two points:

This is not pertinent to this forum. Unless there’s something your formatting is hiding (or more accurately, lack-there-of, see below), you’d most likely have the same issue if you were running this as a Python script from the command line with that code as the content or if you pasted that into an IPython interpreter console. ((Hence, that makes it not a Jupyter issue.\*\*

When you find the appropriate location for your post…  
For posting on forums such as this you’ll want to learn how to use code blocks to keep the formatting of the lines. Without the indentation, it is hard for anyone to help you. See [here](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587), with special attention to ‘Block code formatting’. (More help can be found [here](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#fenced-code-blocks) and [here](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587). That skill will help you posting in here and many other places, such as StackOverflow which may be better suited for your issue.

---

<div class="post-metadata">

### Author: ![tony](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/tony/32/7642_2.png) [@tony](https://discourse.jupyter.org/u/tony)
#### Post date: [December 27, 2023, 6:29pm UTC](https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096/3 "2023-12-27T18:29:24Z")

</div>

fomightez is correct, this forum is not a python help site. If your code ran in Python but not in Jupyter with a python kernel then it would be more interesting to the audience here. It is holiday time here in the US, so I had time to get your code running in my jupyterhub with Python3 kernel.

```auto
MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

is_on = True
money = 0

def is_sufficient(order):
    for ingredient in MENU[order]["ingredients"]:
        if resources[ingredient] < MENU[order]["ingredients"][ingredient]:
            print(f"There is not enough {ingredient} to make {order}")
            return False
    return True

def transaction(order, quarters, dimes, nickles, pennies):
    total = quarters * 0.25 + dimes * 0.1 + nickles * 0.05 + pennies * 0.01
    cost = MENU[order]["cost"]
    if total >= cost:
        change = total - cost
        print(f"Here is your change ${change:.2f}")
        global money
        money += cost
        return True
    else:
        print("Sorry that's not enough money. Money refunded")
        return False

while is_on:
    order = input("What would you like? ")

    if order == "report":
        print(resources)
        print(f"Money: ${money:.2f}")
    else:
        if is_sufficient(order):
            print("Amount you owe for the", order, "is: $", MENU[order]["cost"])
            print("Please insert coins")
            quarters = int(input("Quarters: "))
            dimes = int(input("Dimes: "))
            nickles = int(input("Nickles: "))
            pennies = int(input("Pennies: "))

            if transaction(order, quarters, dimes, nickles, pennies):
                for ingredient in MENU[order]["ingredients"]:
                    resources[ingredient] -= MENU[order]["ingredients"][ingredient]
                print("Here is your drink. Enjoy!")

```

---

<div class="post-metadata">

### Author: ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)
#### Post date: [December 27, 2023, 7:35pm UTC](https://discourse.jupyter.org/t/help-i-get-stuck-at-this-code-dont-know-why-is-sufficient-false-not-working/23096/4 "2023-12-27T19:35:46Z")

</div>

> [@tony](#):
>
> I had time to get your code running in my jupyterhub with Python3 kernel.
> 
> ```auto
> 
> ```

This is great, Tony!

Now that I can better see the structure of the original code vs. what you have working, I think I note a couple thins that may help 12N1-36-Lam\_M\_Van understand some of what was going on originally:

12N1-36-Lam\_M\_Van, I think for `is_sufficient` you, were originally setting as a boolean. Then it seems you were trying to make a separate function `is_sufficient()`, too. So when your code hit trying to use the function that you already assigned as boolean, Python was getting messed up. I cannot quite tell what you would have hit first. Maybe a key error? But whatever you were seeing, the confusion there was probably the issue you were bringing up in the title of your post.

Also , 12N1-36-Lam\_M\_Van, you’ll note Tony uses `global money` in the transaction function. This is because Tony’s implementation of your code tries to adjust. The easy solution is to just make the money used in the function scope the global one; however, that is generally frowned upon, see [here](https://stackoverflow.com/a/19158418/8508004). Ideally, you’d explicitly pass in `money` to the transaction function and return `money` adjusted among what the transaction function returns. This keeps things clearer than using a global. You were trying to return `money +cost` and that may have gotten around that issue? I cannot specifically tell. The fact Tony had to resort to using a global means things weren’t as ‘clean’ as they could be. A few globals can be fine, especially when using object oriented code or an animation, but usually it means your functions are not implemented as explicitly as they should ideally be. Usually though you don’t need things ideal unless you are making production code that will be built on extensively by others.
