numpy.ndarray 'Objekt hat kein Attribut' anhängen
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)
LISTE 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)
Dies führt dazu, dass das Objekt 'numpy.ndarray' immer wieder das Attribut 'append' hat. Ich möchte Benutzerdaten zum Array test11 hinzufügen können. Funktioniert einwandfrei, ohne test11 zu einem numpy-Array zu machen. Aber ich möchte die Größe der Zahl auf 20 begrenzen können. Irgendwelche Ideen? Bitte machen Sie es einfach.
FEHLERCODE: Traceback (letzter Aufruf zuletzt): Zeile 10 in test11.append (Daten) AttributeError: Das Objekt 'numpy.ndarray' hat kein Attribut 'append'.
Antworten
Das funktioniert. Ich habe wieder ein paar Zeilen geändert und markiert.
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)
Listenversion.
# 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)
Dies ist so komprimiert, wie ich es schaffen könnte, ohne das Ganze zu ändern und es ähnlich zu halten, wie Sie es begonnen haben.
Jetzt kann der Benutzer keine Zahl über 20 oder Buchstaben verwenden, da sonst ein Fehler auftritt.
Numpy Arrays haben keine 'Append'-Methode, sie sollen mit der Form und Länge erstellt werden, die Sie benötigen. In Ihrem Fall besteht der beste Weg darin, eine Liste zu erstellen, die Werte anzuhängen und dann das Array zu erstellen:
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)
Sie können Ihre Zeile ersetzen durch np.append:
np.append(test11,data)
Das Anhängen an ein numpy-Array ist jedoch teurer als das Anhängen an eine Liste. Ich würde vorschlagen, listStruktur mit zu verwenden appendund am Ende Ihre Liste in numpy Array mit zu konvertierennp.array
Hier ist eine Listenversion:
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)