+ 2
AttributeError: 'tuple' object has no attribute 'append'
So iāve learned the first 2 modules of python and i wanted to write a program where you could type some numbers and it would give you the average of them. I had an error āAttributeError: 'tuple' object has no attribute 'append'ā at line 22 and i looked it up, because i couldnāt figuer out what i did wrong. Iāve learned that a tuple is like a list but you canāt add anything and that it doesnāt use square brackets and i do clearly in line 4, so I canāt figuer out what i did wrong. Could anyone explain my mistake? https://code.sololearn.com/cCF62uJ2VMs2/?ref=app
12 Answers
+ 8
Max Poppe I haven't figured out the problems with your code, but I just wanted to thank/congratulate you for the quality of your question. It would be nice if everyone took the time to post thoughtful questions like that.
+ 7
You have commas at the end of lines 1-4.
1: print("to stop adding numbers type 'quit' "),
2: input_user = input("type here your number:"),
3: result = 0,
4: input_numbers = [],
I think Python automatically creates a tuple for lines 2 & 4 because without the parenthesis this is normally a multi-assignment syntax:
a,b = b,a
...but without two variables on the left, Python took the 'safe' route, a tuple.
Line 1 appears to be ignored like the second part is an empty line, and
You overwrite line 3's result later, so it doesn't cause a problem.
+ 7
The code that calculates your average is indented at the same/deeper level as an earlier always-'break'. This makes it unreachable code. (Hint)
+ 3
Princetronixx wow iām so embarassed right now. In my other code I did it right, well thank you so much for helping me. aparently Iāve also a mathematical mistake but at least I get a number now, I only devide the first number of the list, because i reset the sum every time, but i fixed it so thank you very much
+ 2
There's not a single mistake, but there are a number of mistakes, firstly if you want to take multiple input, you have to include it inside the loop or use split().Check my code:
https://code.sololearn.com/cpChbcip2utc/?ref=app
+ 2
David Ashton well thanks anyways for your effort and looking at my code.
+ 2
How do you feel about functions? Like, do you have any practice with them, can you use them, do you have to stick with this format?
+ 1
Kirk Schafer thanks for the hint not that I really know what that means, but I will try. I know how to make a code that works but Iāve been trying to fix this one for too long just to give up so Iāll stick with the mechanism of this code.
+ 1
Kirk Schafer iām only at module 2 Iād love to learn more, but Iām to busy to add more things to my sheet where i write everything Iāve learned so I took a break until I can put it on there, but I havenāt learned about the function ādefā ect so this it what I have to do it with. For someone who knows the entire language it would probably be easier, but i like challenges.
+ 1
your code contains bugs and errors on lines
7: (bug), if the user inputs "quit" the program
must calculate the average but the problem is
you *Break* before the calculation that is why
it always says average is zero
8 & 9 : (error) these lines are not correct
the right way to call len is len(input_numbers)
11 :(error) the correct way to access members of a list is by using square brackects [ ] that is
addition_number = input_numbers [index]
0
Kirk Schafer that could be, I honestly do not know, but without the commaās iāve another problem because Iāve used too much memory or time or something like that. so after trying out a bunch of things I tried a comma and my problem went away. On python itselve it sais that it canāt run multiple variables at a time with the error āSyntaxError: multiple statements found while compiling a single statementā
0
Thank you for your help, I actually tried to put it in before, but I still had errors so I removed it back to itās original spot (outside) the while statement. Iāll keep it in there and try to figuer out the last few bugs, because now it sais that my result is 0 instead of the average of my numbers.