[英]AttributeError: 'builtin_function_or_method' object has no attribute 'pop'
Here is my code 这是我的代码
def allButMax():
input_list=[]
user_input = 0
num_sum = 0
max_num = 0
while user_input != "end":
input_list.append(float(user_input))
user_input = input("Enter next number: ")
input_list = input_list[1:]
input_list = input_list.sort
input_listnew = input_list.pop(-1)
for num in input_list:
num_sum = num_sum + num
print("The num of all values except for the maximum value is: ", num_sum)
When I run the program, it says 当我运行程序时,它说
AttributeError: 'builtin_function_or_method' object has no attribute 'pop' AttributeError:“ builtin_function_or_method”对象没有属性“ pop”
This program computes the sum of all the values entered except for the maximum value in the series 该程序将计算除序列中的最大值外所有输入值的总和
I do not know where I went wrong 我不知道我哪里出了错
The problem is on this line 问题在这条线上
input_list = input_list.sort
input_list.sort
is not actually being called. 实际上并未调用input_list.sort
。 You are just storing a reference to the function in input_list
and consequently nuking the list. 您只是将对函数的引用存储在input_list
,因此对列表进行了input_list
。
sort
is an inplace method, it operates on the list itself, rather than returning a new sorted copy of the list (see sorted
). sort
是一个就地方法,它对列表本身进行操作,而不是返回列表的新排序副本(请参见sorted
)。 It returns None
therefore you should not try to assign from its return value. 它返回None
因此您不应尝试从其返回值进行赋值。
replace 更换
input_list = input_list.sort
with 与
input_list.sort()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.