Reputation: 21
'int' object has no attribute 'append'`
The error message listed above appears and I've been trying to fix it, but don't know or why it does this. I have other sections in my code that are exactly like this, minus the variable names, and they work fine.
How can I fix this code?
def vf():
q2 = input("How many questions (number) : ")
i = list()
i2 = list()
for i in range(0, int(q2)):
x2 = input("Enter question : ")
y2 = input ("Enter answer. (True or false?) : ")
i.append(x2)
i2.append(y2)
list(zip(x2, y2))
vfqr = input("Would you like to save? (yes or no) ")
if (vfqr == 'yes' ) :
print ("Saving...")
from time import sleep
sleep(1.5)
x2 = i
import pickle
pickle.dump(x2, open("vfx.dat", "wb"))
y2 = i2
import pickle
pickle.dump(y2, open("vfy.dat", "wb"))
q2 = q2
import pickle
pickle.dump(q2, open("vfqr.dat", "wb"))
print ("Enregistré!")
print("Sélectionner «ouvrir» puis «vrai ou faux» pour utiliser ces données.")
if (vfqr == 'no' ) :
print ("Pleas try another option or close the programme")
Upvotes: 0
Views: 146
Reputation: 4432
You are redefined i
variable in for
loop and it become not a list but an integer. You should either change list var name or use something like this in your for loop:
for idx in range(0, int(q2)): ...
There are other problems you have there like redundant code with result never assigned to the variable:
list(zip(x2, y2))
Or using user input without any validation in for loop (what if user enter a letter or zero?):
range(0, int(q2))
Upvotes: 1
Reputation: 93
I found the error in your code.
You have:
def vf():
q2 = input("How many questions (number) : ")
i = list()
i2 = list()
for i in range(0, int(q2)):
x2 = input("Enter question : ")
y2 = input ("Enter answer. (True or false?) : ")
i.append(x2)
which is problematic, because you set your list variable to i
, but you're also using i
to iterate in the for loop! (to clarify, you have i = list()
and for i in range
).
I've taken your code and changed the variable to something else and it works just fine.
def vf():
q2 = input("How many questions (number) : ")
iter_list = list()
i2 = list()
for i in range(0, int(q2)):
x2 = input("Enter question : ")
y2 = input ("Enter answer. (True or false?) : ")
iter_list.append(x2)
i2.append(y2)
list(zip(x2, y2))
vfqr = input("Would you like to save? (yes or no) ")
if (vfqr == 'yes' ) :
print ("Saving...")
from time import sleep
sleep(1.5)
x2 = i
import pickle
pickle.dump(x2, open("vfx.dat", "wb"))
y2 = i2
import pickle
pickle.dump(y2, open("vfy.dat", "wb"))
q2 = q2
import pickle
pickle.dump(q2, open("vfqr.dat", "wb"))
print ("Enregistré!")
print("Sélectionner «ouvrir» puis «vrai ou faux» pour utiliser ces données.")
if (vfqr == 'no' ) :
print ("Pleas try another option or close the programme")
vf()
Upvotes: 2
Reputation: 473
def vf():
q2 = int(input("How many questions (number) : "))
i_list = []
i2_list = []
for i in range(0, q2):
x2 = input("Enter question : ")
y2 = input ("Enter answer. (True or false?) : ")
i_list.append(x2)
i2_list.append(y2)
list(zip(i_list, i2_list))
vfqr = input("Would you like to save? (yes or no) ")
if (vfqr == 'yes' ) :
print ("Saving...")
from time import sleep
sleep(1.5)
x2 = i_list
import pickle
pickle.dump(x2, open("vfx.dat", "wb"))
y2 = i2_list
import pickle
pickle.dump(y2, open("vfy.dat", "wb"))
q2 = q2
import pickle
pickle.dump(q2, open("vfqr.dat", "wb"))
print ("Enregistré!")
print("Sélectionner «ouvrir» puis «vrai ou faux» pour utiliser ces données.")
if (vfqr == 'no' ) :
print ("Pleas try another option or close the programme")
Upvotes: 0
Reputation: 456
Next time it would help if you gave the whole error message since sometimes in large codes, and even in small codes, it can be confusing to find exactly where the error is. If you give the whole error, those kinds of things can be found quicker.
Anyway, your problem here is something a lot of people encounter at some point. You first assign i
to list()
, and then reassign it in the for loop. The range function causes it to be an int everytime it runs the loop, hence you error, 'int' object has no attribute 'append'
. I suggest that in the future you use more descriptive variables. Not only will this make your code easier to understand, but if you want to find and replace it with another variable, it will be easier to find just that variable instead of others as well.
Upvotes: 1