Objek numpy.ndarray 'tidak memiliki atribut' 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)
VERSI DAFTAR
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)
Hal ini membuat objek 'numpy.ndarray' tidak memiliki atribut 'append.' Saya ingin dapat menambahkan data pengguna, ke array test11. Berfungsi dengan baik tanpa membuat test11 menjadi array numpy. Tapi saya ingin membatasi ukuran angkanya menjadi 20. Ada ide? Tolong buat itu sederhana.
KODE EROR: Traceback (panggilan terakhir terakhir): baris 10, di test11.append (data) AttributeError: objek 'numpy.ndarray' tidak memiliki atribut 'append'
Jawaban
Lapisan ini bekerja. Saya mengubah beberapa baris lagi dan menandainya.
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)
Versi daftar.
# 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)
Ini sekaduh yang saya bisa, tanpa mengubah semuanya, dan menjaganya tetap sama dengan yang Anda mulai.
Sekarang pengguna tidak dapat menggunakan angka di atas 20, atau huruf apa pun, atau kesalahan akan muncul.
Array numpy tidak memiliki metode 'append', mereka seharusnya dibuat dengan bentuk dan panjang yang Anda perlukan. Jadi dalam kasus Anda, cara terbaik adalah membuat daftar, menambahkan nilai, dan kemudian membuat array:
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)
Anda dapat mengganti baris Anda dengan np.append
:
np.append(test11,data)
Tetapi menambahkan ke numpy array lebih mahal daripada menambahkan ke daftar. Saya akan menyarankan menggunakan list
struktur dengan append
dan pada akhirnya mengubah daftar Anda menjadi array numpy menggunakannp.array
Ini adalah versi daftar:
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)