Python Forum
BAH! Need help with function!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BAH! Need help with function!
#1
So I'm still working on this darn program lol, I got it to run but when I go to delete an item it's saying del_item is not defined but I'm confused because I swear it is! Here is the code and the error(I know global variables are not good programming it's just what the professor wants >.< lol)

# banner
print("Hello, welcome to my calorie counter program")

# create variables
item_cnt = (0)		# this is the item counter
tot_cals = (0)	# this is the total meal calories

itemNameGlobal = ""
itemCalsGlobal = 0



item_list = [] #Define the item list, left it empty since the user inputs the item
cals_list = [] #Cals list

def disp_menu():
    choice_list = ["a", "d", "m", "q"]

    while True:
        print("What would you like to do")
        print("a = add an item")
        print("d = delete an item")
        print("m = display the meal so far")
        print("q = quit")
        choice = input("make a selection>")

        if choice in choice_list:
            return choice
        else:
            print("that is not a valid entry, please try again.")
	



def disp_meal():
    print("\nMeal Calorie Counter")
    print("Num\tItem\t\tCals")
    print("---\t----\t\t---")
    meal_cals = 0

    for c in range(len(item_list)):
        meal_cals += cals_list[c]
        print("{}.\t{}\t\t{}".format(c+1, item_list[c], cals_list[c]))

    print("\nYour meal has {} items for a total {} calories\n".format(len(item_list), meal_cals))
    print("-" *  20)




def enterItemName():
        valid_data = False
	#capture input
        while not valid_data:
                item_name = input("please enter the item>")
                if len(item_name) > 20:
                        print("not a valid food name")
                elif len(item_name) == 0:
                        print("you need to enter a name")
                else:    # item_name is valid 
                        return item_name

def enterElementGrams(element):
	valid_data = False
	while not valid_data:
		try:
			grams = int(input("enter grams of {}> ".format(element)))
		except Exception as detail:
			print("{} sorry that is not a valid entry: ".format(element)) #I left out the detail so the user doesn't go what!?? If they recieve this error
		else:
			return grams
		
def math(element, grams):
	if element == "f":
		return grams * 9
	else:
		return grams * 4

def add_process(tot_cals, item_cnt):
    # input data
    item_name = enterItemName()
    g_carbs = enterElementGrams("carbs")
    g_fats = enterElementGrams("fats")
    g_prot = enterElementGrams("proteins")

    cals = math("c", g_carbs) + math("f", g_fats) + math("p", g_prot)
    # output

    print("total calories for {} are {}".format(item_name, cals))
	
    incl = input("would you like to include {}? (y/n)".format(item_name))

    if incl.lower() == "y":
        addItem(item_name, cals)
            
        tot_cals = tot_cals + cals
        item_cnt += 1
        print("item {} entered.".format(item_name))
    else:
        print("item {} not entered.".format(item_name))

    return tot_cals, item_cnt

                    
def addItem(name, cals):
        item_list.append(name)
        cals_list.append(cals)

def del_Item():
    if len(item_list) == 0:
        print("you have no items in your menu to delete")
    else:
        print("\nDelete an item")
        disp_meal()

        valid_data = False
        while not valid_data:
            try:
                choice = int(input("Please enter the item number you would like to delete>"))
                if 1<= choice <= len(item_list):

                    choice = choice - 1

                    print("item {}. {} with {} calories will be deleted".format(choice+1,
                                                                                            item_list[choice]
                                                                                            ,cals_list[choice]))
                    del item_list[choice]
                    del cals_list[choice]
                    valid_data = True

            except Exception as detail:
                   print("error: ", detail)
                   print("please try again")
             


# Main program
while True:

    choice = disp_menu()

    if choice == "a":
         tot_cals , item_cnt = add_process(tot_cals, item_cnt)
    elif choice == "d":
          del_item()
    elif choice == "q":
        break

    disp_meal()
	


disp_meal()
print("Thank you for using our program")
Hello, welcome to my calorie counter program
What would you like to do
a = add an item
d = delete an item
m = display the meal so far
q = quit
make a selection>a
please enter the item>bacon
enter grams of carbs> 5
enter grams of fats> 5
enter grams of proteins> 4
total calories for bacon are 81
would you like to include bacon? (y/n)y
item bacon entered.

Meal Calorie Counter
Num Item Cals
--- ---- ---
1. bacon 81

Your meal has 1 items for a total 81 calories

--------------------
What would you like to do
a = add an item
d = delete an item
m = display the meal so far
q = quit
make a selection>d
Error:
Traceback (most recent call last): File "C:\Users\Justus\Creative Cloud Files (archived) (1)\Desktop\programming\Python\Lesson8Cal.py", line 151, in <module> del_item() NameError: name 'del_item' is not defined >>>
Reply
#2
Python is very serious about letter case, so compare the function definition to the function call that causes the error ;P
Reply
#3
(Mar-04-2018, 05:35 PM)j.crater Wrote: Python is very serious about letter case, so compare the function definition to the function call that causes the error ;P
yeah I went through and checked but everything matches up so I'm not sure why it's telling me its not defined. :/
Reply
#4
Unless you corrected the code afterwards, it doesn't match:
line 109 = del_Item
line 154 = del_item
Reply


Forum Jump:

User Panel Messages

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