[英]numpy.ndarray' object has no attribute 'append
import numpy as np
Student1= [1,2]
test11= np.array([])
np.clip(0,1,20)
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
data=int(input())
test11.append(data)
total=0
for value in test11:
total=total+value
print("The sum of all", total)
LIST VERSION列表版本
import numpy as np
Student1= [1,2]
test11= []
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
data=int(input())
test11.append(data)
total=0
for value in test11:
total=total+value
print("The sum of all", total)
This keeps erroring 'numpy.ndarray' object has no attribute 'append.'这不断出错 'numpy.ndarray' object has no attribute 'append.' I wanna be able to add user data, to the array test11.
我希望能够将用户数据添加到数组 test11。 Works fine without making test11 a numpy array.
无需将 test11 设为 numpy 数组即可正常工作。 But I wanna be able to limit the size of the number to 20. Any ideas?
但我希望能够将数字的大小限制为 20。有什么想法吗? Plz make it simple.
请简单点。
ERROR CODE: Traceback (most recent call last): line 10, in test11.append(data) AttributeError: 'numpy.ndarray' object has no attribute 'append'错误代码:回溯(最近一次调用):第 10 行,在 test11.append(data) AttributeError: 'numpy.ndarray' object 中没有属性 'append'
This seams to work.这接缝工作。 I changed a few lines again and marked them.
我又改了几行并做了标记。
import numpy as np
Student1= [1,2]
test11= np.array([0])
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
data = None
while data is None: # Remove this if you want the program to end if an error occurres.
for i in range(NOF):
try: # Be sure the input is a int.
data=np.array([int(input())])
if data > 20: # Be sure the input is <= 20.
data = None # If greater then 20. Turn data into None
test11 = np.array([0]) # Empty the array.
print("Error")
break # Then break out of the loop
test11 = np.append(data, test11)
except ValueError:
data = None # If it is not a int, data will trun into None
test11 = np.array([0]) # Empty the array.
print("Error")
break
if data is not None: # If data is not None then find the sum.
total=0
for value in test11:
total = test11.sum() # Use the sum fuction, though total=total+value will also work.
print("The sum of all", total)
List version.列出版本。
# import numpy as np
Student1= [1,2]
test11= []
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
data = None # Assign ahead of time.
while data is None: # Remove this if you want the program to end if an
error occurres.
for i in range(NOF):
try: # Be sure the input is a int.
data=int(input())
if data > 20: # Be sure the input is <= 20.
data = None # If greater then 20. Turn data into None
test11.clear() # Clear the list if an error occurres.
print("Error")
break # Then break out of the loop
test11.append(data)
except ValueError:
data = None # If it is not a int, data will trun into None
test11.clear() # Clear the list if an error occurres.
print("Error")
break
if data is not None: # If data is not None then find the sum.
total=0
for value in test11:
total=total+value
print("The sum of all", total)
This is as compacted as I could make it, without changing the entire thing, and keeping it similar to what you started with.这是我能做到的最紧凑的,没有改变整个东西,并保持它与你开始时的相似。
Now the user can't use a number over 20, or any letters, or an error will appear.现在用户不能使用超过 20 的数字,或者任何字母,否则会出现错误。
Numpy arrays doesn't have 'append' method, they are supposed to be created with the shape and length that you will need. Numpy arrays 没有“附加”方法,它们应该使用您需要的形状和长度创建。 So in your case, the best way would be to make a list, append the values, and then creating the array:
所以在你的情况下,最好的方法是制作一个列表, append 个值,然后创建数组:
import numpy as np
Student1= [1,2]
test11= []
np.clip(0,1,20)
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
data=int(input())
test11.append(data)
test11 = np.array(test11)
You can replace you line with np.append
:您可以将行替换为
np.append
:
np.append(test11,data)
But appending to numpy array is costlier than appending to a list.但是附加到 numpy 数组比附加到列表更昂贵。 I would suggest use
list
structure with append
and at the end convert your list to numpy array using np.array
我建议将
list
结构与append
一起使用,最后使用np.array
将列表转换为 numpy 数组
Here is a list version:这是一个列表版本:
import numpy as np
Student1= [1,2]
test11= []
np.clip(0,1,20)
NOF=int(2)
print("Enter test score, students name are (1,2,3, etc): ")
for i in range(NOF):
data=int(input())
test11.append(data)
test11 = np.array(test11)
total = test11.sum()
print("The sum of all", total)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.