numpy.ndarray 'đối tượng không có thuộc tính' phụ thêm
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)
PHIÊN BẢN DANH SÁCH
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)
Điều này khiến đối tượng 'numpy.ndarray' không có thuộc tính 'append'. Tôi muốn có thể thêm dữ liệu người dùng vào thử nghiệm mảng11. Hoạt động tốt mà không làm cho test11 trở thành một mảng phức tạp. Nhưng tôi muốn có thể giới hạn kích thước của con số là 20. Có ý kiến gì không? Làm cho nó đơn giản.
MÃ LỖI: Traceback (cuộc gọi gần đây nhất): dòng 10, trong test11.append (data) AttributeError: đối tượng 'numpy.ndarray' không có thuộc tính 'append'
Trả lời
Đường nối này để làm việc. Tôi lại thay đổi một vài dòng và đánh dấu chúng.
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)
Phiên bản danh sách.
# 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)
Điều này được nén chặt nhất có thể mà tôi có thể làm, mà không thay đổi toàn bộ và giữ nó giống với những gì bạn đã bắt đầu.
Bây giờ người dùng không thể sử dụng một số trên 20, hoặc bất kỳ chữ cái nào hoặc lỗi sẽ xuất hiện.
Mảng Numpy không có phương thức 'append', chúng được cho là được tạo với hình dạng và độ dài mà bạn cần. Vì vậy, trong trường hợp của bạn, cách tốt nhất là tạo một danh sách, nối các giá trị và sau đó tạo mảng:
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)
Bạn có thể thay thế bạn bằng np.append:
np.append(test11,data)
Nhưng việc thêm vào mảng numpy sẽ tốn kém hơn việc thêm vào một danh sách. Tôi sẽ đề xuất sử dụng listcấu trúc có appendvà ở cuối chuyển đổi danh sách của bạn thành mảng numpy bằng cách sử dụngnp.array
Đây là một phiên bản danh sách:
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)