0
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.' I wanna be able to add user data, to the array test11. Works fine without making test11 a numpy array. But I wanna be able to limit the size of the number to 20. Any ideas? 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'

3
  • 2
    numpy.ndarray objects indeed do not have an append method. Why don't you ust use a list? Commented Aug 18, 2020 at 23:09
  • Please provide the entire error output, as well as a minimal reproducible example. Commented Aug 18, 2020 at 23:23
  • That np.clip line does nothing for you. Reread its docs. Commented Aug 19, 2020 at 1:53

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I didn't say it correctly. The clip function is useless coz I would like the code to error if the user types a number bigger then 20
if you could help with that great. If not I thank you a lot for helping me implement the np.
We discourage the use of np.append. List append is faster and easier to use right.
On top of hpauli's comment I would like to add: you do not need to create an array to append a number to array. And your second for loop is irrelevant! The loop is doing same thing repeatedly without any purpose. Besides you do not need to set total=0 before a loop that you are going to assign a new value.
Thx again a lot for help
|
0

Numpy arrays doesn't have 'append' method, they are supposed to be created with the shape and length that you will need. So in your case, the best way would be to make a list, append the values, and then creating the 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)

Comments

0

You can replace you line with np.append:

np.append(test11,data)

But appending to numpy array is costlier than appending to a list. I would suggest use list structure with append and at the end convert your list to numpy array using np.array

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)

8 Comments

When I replace it with 'np.append(test11,data)' it doesn't add up anymore :(
I will go ahead an try the list method
Did the list but where do I type the np.array into
@GlitterPony Added the list version to post
But when I use the list I can't limit the size of the number to 20 (not the size of the list)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.