Python Forum
Function not scriptable: Noob question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function not scriptable: Noob question
#1
Hey everyone.
I keep getting the error "Function no scriptable". I don't know why.

from tkinter import Menu


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,
    }
}

profit = 0

resources = {
    "water": 0,
    "milk": 0,
    "coffee": 0,
    "money": 0.00
}

def checkr(drink):
    make_drink = True
    if resources["water"] < MENU[choice]["ingredients"]["water"]:
        print("There is not enough water")
        make_drink = False
    if resources["milk"] < MENU[choice]["ingredients"]["milk"]:
        print("There is not enough milk")
        make_drink = False
    if resources["coffee"] < MENU[choice]["ingredients"]["coffee"]:
        print("There is not enough coffee grounds")
        make_drink = False
    return make_drink
        

is_on = True

while is_on:
    choice = input("What would you like? (espresso/latte/cappuccino): ")
    if choice == "off":
        is_on = False
    if choice == "report":
        print(f"Water: {resources['water']} ml")
        print(f"Milk: {resources['milk']} ml")
        print(f"Coffee: {resources['coffee']} ml")
        print(f"Money: ${resources['money']}")
    if choice == "latte" or choice == "espresso" or choice == "cappuccino":
        if checkr[choice] == True:
            print("Drink Made")
Reply
#2
When posting an error please give the full error traceback in error tags

You have the wrong type of brackets on the following line, the [] brackets
if checkr[choice] == True:
should be ()
if checkr(choice) == True:
BashBedlam likes this post
Reply
#3
The error is "TypeError: 'function' object is not subscriptable"

It is telling you that you are trying to use "subscripts" with a function object. Square brackets are used to subscript, or index, "subscriptable" objects like lists or dictionaries. Parenthesis are used to call a function.

This is bad programming. It is too restrictive and doesn't use the information available in the MENU items.
def checkr(drink):
    make_drink = True
    if resources["water"] < MENU[choice]["ingredients"]["water"]:
        print("There is not enough water")
        make_drink = False
    if resources["milk"] < MENU[choice]["ingredients"]["milk"]:
        print("There is not enough milk")
        make_drink = False
    if resources["coffee"] < MENU[choice]["ingredients"]["coffee"]:
        print("There is not enough coffee grounds")
        make_drink = False
    return make_drink
The checkr function forces all drinks to use the same ingredients. If you add strawberry smoothies to your menu, you'll have to rewrite the checkr() function to check for strawberries, and all menu items will have to be modified to include strawberries.

I write checkr() something like this:
def checkr(drink):
    # Check if there are enough ingredients
    for ingredient, amount in MENU[drink]["ingredients"].items():
        if resources[ingredient] < amount:
            print(f"There is not enough {ingredient}")
            return False
    return True
Now checkr() only checks ingredients for the drink you are making. If you added fruit smoothies to your menu you would not have to rewrite checkr to check for strawberries, nor would you have to add them to all your menu ingredients. I would do something similar with the "report" action. The code that prints an inventory report should not have to know every possible ingredient. That information is already in the ingredients dictionary.
Reply
#4
Thank you so much!

I'm sorry for not posting logs. I knew I was just doing something really stupid and couldn't figure it out. I kept running into the error intermittently.

Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on dir() function Soorya25 1 1,135 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,376 Sep-07-2022, 02:01 AM
Last Post: kaega2
  input function question barryjo 12 2,701 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Question on None function in a machine learning algorithm Livingstone1337 1 2,352 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  Noob question about lists adifrank 4 2,863 Nov-19-2020, 03:26 AM
Last Post: adifrank
  Noob question: why is shapesize() not working for my turtle adifrank 8 5,745 Sep-09-2020, 11:13 PM
Last Post: adifrank
  Noob question adifrank 6 2,730 Aug-18-2020, 11:50 PM
Last Post: adifrank
  Question in python function problem saratha 1 1,447 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 2,620 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about the groupby function new_to_python 7 3,200 Feb-09-2020, 07:52 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020