V122 V122 - 2 years ago 483
Python Question

Python: ‘tuple’ object is not callable

Here is my code:

# Major Functions
cont = str("y")
disp_menu = ()
add_process = ("", "")
calc_cals = ()
input_grams = ()
add_item = ("", "")
del_item = ()
input_name = ()
tot_cals = ()
item_cnt = ()
item_name = ()
cals = ()

# Displaying the menu
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
print("I do not understand that entry. Try again")

# List
item_list = []
cals_list = []

# Input item name
def input_name():
valid_data = False
while not valid_data:
item_name = input("Please enter the item name> ")
if len(item_name) > 20:
print("Not a valid food name.")
elif len(item_name) == 0:
print("You need to enter an item name.")
valid_data = True
return input_name

# Input of grams
def input_grams(element):
valid_data = False
while not valid_data:
grams = int(input("Enter grams of {}> ".format(element)))
valid_data = True
except Exception as detail:
print("{} error:".format(element, detail))
return grams

# Item input process
def add_process(tot_cals, item_cnt):
# Input data
item_name = input_name()
g_carbs = input_grams("carbs")
g_fats = input_grams("fats")
g_prot = input_grams("proteins")
# Do the math
cals = calc_cals("c", g_carbs) + calc_cals("f", g_fats) + calc_cals("p", g_prot),
# Output
print("Total calories for {} are {}".format(item_name, cals))
# Prompt to include item
incl = input("Would you like to include {}? (y/n)> ".format(item_name))
if incl.lower() == "y":
add_item(item_name, cals)
# Accumulate totals
tot_cals = tot_cals + cals
item_cnt += 1
print("Item {} entered.".format(item_name))
print("Item {} not entered.".format(item_name))
return tot_cals, item_cnt
# Deleting an item
def del_item():
if len(item_list) == 0:
print("You have no items in your menu to delete")
print("/nDelete and Item")
valid_data = False
while not valid_data:
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,
del item_list[choice]
del cals_lsit[choice]
valid_data = True
except Exception as detail:
print("Error: ", detail)
print("Please try again.")

# Adding an item
def add_item(item_name, cals):
# add to lists

# Displaying the meal
def disp_meal():
print("\nMeal Calorie Counter")
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 of {} calories\n".format(len(item_list), meal_cals))
print("-" * 20)

# Main Program
while True:
choice = disp_menu()
if choice == "a":
tot_cals, item_cnt = add_process(tot_cals, item_cnt)
elif choice == "d":
elif choice == "q":
# Prompt to add another item
#cont = input("Would you like to add another item? (y/n)> ")
# Thank you message

I keep on getting this message and can't seem to find the problem!!

Traceback (most recent call last):
File "C:\Users\V122\Desktop\lesson 8.py", line 144, in <module>
add_process(tot_cals, item_cnt)
File "C:\Users\V122\Desktop\lesson 8.py", line 79, in add_process
cals = calc_cals("c", g_carbs) + calc_cals("f", g_fats) + calc_cals("p", g_prot)
TypeError: 'tuple' object is not callable"

Answer Source

As you have it shown here, calc_cals = () is defined at the top of the program, but you've defined it as an empty tuple, not as a function. When you try to use it as a function, you get that error because you're trying to use a tuple as a function.

You either need to define the calc_cals complete function in this program file, or import another file that contains your program definitions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download