如何将整数放在列表的末尾,使其不会变成 Python 中的 "Attribute Error"?
How do you put integers onto the end of a list so that it does not become an "Attribute Error" in Python?
我正在制作一个平均值计算器,它计算用户选择的数字的平均数。到目前为止,这是我的代码:
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))
预期结果:
How many numbers would you like the calculate the average of?
>>>2
What is Number 1?
>>>5
What is Number 2?
>>>8
2
到目前为止,我正在尝试查看它是否放在列表的末尾,但它最终变成了 Attribute Error
。
实际结果:
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".
当您遇到此类属性错误时,请确保对象具有正确的类型。错误说它是一个字符串,而不是一个列表。这是重要信息。
In [132]: n = ("m")
In [133]: n
Out[133]: 'm'
()
只是分组;他们自己不会创建列表(或元组)
In [134]: n = ("m",)
In [135]: n
Out[135]: ('m',)
包含 ,
会创建一个元组。但是元组也没有 extend
方法。
你想要一个列表:
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)
并且您想给 extend
一个列表,而不是多个参数:
In [139]: n.extend(["one","two","three"])
In [140]: n
Out[140]: ['m', 'one', 'two', 'three']
首先,您应该使用 []
而不是 ()
-> NUMBERS = ["m"]
来定义您的列表
然后,append()
函数在列表末尾添加一个元素。
所以,这是经过这两项修改的代码:
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.append(num)
print(NUMBERS(2))
注意: 我不确定为什么要用 "m"
初始化列表,也不明白为什么要在其中添加空字符串 ""
但是这回答了你的问题。如果您提供有关脚本其余部分的更多详细信息,我很乐意提供帮助。
我正在制作一个平均值计算器,它计算用户选择的数字的平均数。到目前为止,这是我的代码:
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))
预期结果:
How many numbers would you like the calculate the average of?
>>>2
What is Number 1?
>>>5
What is Number 2?
>>>8
2
到目前为止,我正在尝试查看它是否放在列表的末尾,但它最终变成了 Attribute Error
。
实际结果:
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".
当您遇到此类属性错误时,请确保对象具有正确的类型。错误说它是一个字符串,而不是一个列表。这是重要信息。
In [132]: n = ("m")
In [133]: n
Out[133]: 'm'
()
只是分组;他们自己不会创建列表(或元组)
In [134]: n = ("m",)
In [135]: n
Out[135]: ('m',)
包含 ,
会创建一个元组。但是元组也没有 extend
方法。
你想要一个列表:
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)
并且您想给 extend
一个列表,而不是多个参数:
In [139]: n.extend(["one","two","three"])
In [140]: n
Out[140]: ['m', 'one', 'two', 'three']
首先,您应该使用 []
而不是 ()
-> NUMBERS = ["m"]
然后,append()
函数在列表末尾添加一个元素。
所以,这是经过这两项修改的代码:
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.append(num)
print(NUMBERS(2))
注意: 我不确定为什么要用 "m"
初始化列表,也不明白为什么要在其中添加空字符串 ""
但是这回答了你的问题。如果您提供有关脚本其余部分的更多详细信息,我很乐意提供帮助。