numpy.ndarray 'objet n'a pas d'attribut' append

Aug 19 2020
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)

VERSION LISTE

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)

L'objet «numpy.ndarray» continue d'errer n'a pas d'attribut «append». Je veux pouvoir ajouter des données utilisateur au tableau test11. Fonctionne bien sans faire de test11 un tableau numpy. Mais je veux pouvoir limiter la taille du nombre à 20. Des idées? Plz le rendre simple.

CODE D'ERREUR: Traceback (dernier appel le plus récent): ligne 10, dans test11.append (data) AttributeError: l'objet 'numpy.ndarray' n'a pas d'attribut 'append'

Réponses

1 ThreePoint14 Aug 19 2020 at 06:50

Cela semble fonctionner. J'ai de nouveau changé quelques lignes et les ai marquées.

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)

Version de la liste.

# 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)

C'est aussi compact que je pourrais le faire, sans changer le tout, et en le gardant similaire à ce avec quoi vous avez commencé.

Désormais, l'utilisateur ne peut pas utiliser un nombre supérieur à 20, ni aucune lettre, ou une erreur apparaîtra.

JoãoVitorBarbosa Aug 19 2020 at 06:22

Les tableaux Numpy n'ont pas de méthode 'append', ils sont censés être créés avec la forme et la longueur dont vous aurez besoin. Donc, dans votre cas, le meilleur moyen serait de faire une liste, d'ajouter les valeurs, puis de créer le tableau:

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)
Ehsan Aug 19 2020 at 06:24

Vous pouvez remplacer votre ligne par np.append:

np.append(test11,data)

Mais l'ajout à un tableau numpy est plus coûteux que l'ajout à une liste. Je suggérerais d'utiliser la liststructure avec appendet à la fin de convertir votre liste en tableau numpy en utilisantnp.array

Voici une version de liste:

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)