j has been used a a list as well as an integer.
Use j only for integer name, name the list to something else.
j.append(filter(isAcceptableChar, j[i])) # j is not a list here,it is an int.
w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
for j in range (0,len(w[i])): # here j is an int
Answer from DhruvPathak on Stack Overflowj has been used a a list as well as an integer.
Use j only for integer name, name the list to something else.
j.append(filter(isAcceptableChar, j[i])) # j is not a list here,it is an int.
w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
for j in range (0,len(w[i])): # here j is an int
Take a close look at the following piece of your code:
for i in range(0,200):
j.append(filter(isAcceptableChar, j[i]))
w.append([word for word in word_tokenize(j[i].lower()) if word not in english_stops])
for j in range (0,len(w[i])):
Notice how you first call .append on j (which you had initialized with a list earlier), then use it as a loop variable nested in the same loop.
Use better, meaningful variable names in your code to avoid this class of errors. Rename either the loop variable or the module-level list variable.
`"object" has no attribute "append"` when adding list to untyped dict
pandas - 'list' object has no attribute 'values' when we are using append in python - Data Science Stack Exchange
AttributeError: 'int' object has no attribute 'values'
'int' object has no attribute 'columns'
Videos
Traceback (most recent call last):
File "E:/CC PART LESSON 811.py123232323.py", line 129, in <module>
tot_cals, item_cnt = add_process(tot_cals, item_cnt)
File "E:/CC PART LESSON 811.py123232323.py", line 100, in add_process
add_item(item_name, cals)
File "E:/CC PART LESSON 811.py123232323.py", line 58, in add_item
item_list.append(name)
AttributeError: 'int' object has no attribute 'append'is the error I get. I have inputs for the user to type things in, and one of them is a name of a food. I want to capture all of their inputs, and append the name to a list. What am I doing wrong? My whole code is:
tot_cals = 0
item_list = 0
item_cnt = int(0)
def calc_cals(g_type, grams):
if g_type == "f":
return grams * 9
else:
return grams * 4
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 of {} calories\n".format(len(item_list), meal_cals))
print("-" * 20)
#Delete an ITEM############
def del_item():
if len(item_list) == 0:
print("you have no items in your menu to delete")
else:
print("delete an Item")
disp_meal()
valid_data = False
while not valid_data:
try:
choice = int(input("please tnter the number you wish 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")
#ADD NAME########
def add_item(name, cals):
item_list.append(name)
cals_list.append(cals)
##################
#INPUT NAME#########
def input_name():
valid_data = False
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:
valid_data = True
return item_name
###################
###INPUT GRAMS
def input_grams(element):
valid_data = False
while not valid_data:
try:
grams = int(input("enter grams of {}".format(element)))
valid_data = True
except Exception as detail:
print("{} error: ".format(element), detail)
return grams
#ADD PROCESS#############
def add_process(tot_cals, item_cnt):
item_name = input_name()
g_carbs = input_grams("carbs")
g_fats = input_grams("fats")
g_prot = input_grams("protein")
cals = calc_cals("c", g_carbs) + calc_cals("f", g_fats) + calc_cals("p", g_prot)
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":
add_item(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))