I am making an average calculator, which calculates the mean average of numbers that are chosen by the user. Here is my code so far:
p = 1
print("How many numbers would you like the calculate the average of?")
k = int(input())
NUMBERS = ("m")
for x in range(k):
u = (x + 1)
print("What is Number ",u,"?",sep="")
num = int(input())
NUMBERS.extend("",num,"")
print(NUMBERS(2))
Expected Result:
How many numbers would you like the calculate the average of?
>>>2
What is Number 1?
>>>5
What is Number 2?
>>>8
2
So far, I am trying to see whether it is put onto the end of the list, but it ends up as an Attribute Error
.
Actual Result:
How many numbers would you like the calculate the average of?
>>>2
What is Number 1?
>>>5
"Line 12, in NUMBERS.extend("",num,"") AttributeError: "str" object has no attribute "extend".
When you have attribute errors like this, make sure the object has the correct type. The error says it's a string, not a list. That's important information.
In [132]: n = ("m")
In [133]: n
Out[133]: 'm'
()
just group things; by themselves they don't create a list (or tuple)
In [134]: n = ("m",)
In [135]: n
Out[135]: ('m',)
Inclusion of a ,
creates a tuple. But a tuple does not have an extend
method either.
You want a list:
In [136]: n = ["m"]
In [137]: n
Out[137]: ['m']
In [138]: n.extend(1,2,3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-138-7b03428a3fa3> in <module>
----> 1 n.extend(1,2,3)
TypeError: extend() takes exactly one argument (3 given)
and you want to give extend
a list, not multiple arguments:
In [139]: n.extend(["one","two","three"])
In [140]: n
Out[140]: ['m', 'one', 'two', 'three']