Help! I get stuck at this code ( don't know why is_sufficient = False not working

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 =quarters0.25+dimes0.1+ nickles0.05 +pennies0.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”)

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 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, with special attention to ‘Block code formatting’. (More help can be found here and here. That skill will help you posting in here and many other places, such as StackOverflow which may be better suited for your issue.

1 Like

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.

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!")

1 Like

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

1 Like