numpy.ndarray 'object ไม่มีแอตทริบิวต์' ต่อท้าย
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)
รุ่นรายการ
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)
สิ่งนี้ทำให้เกิดข้อผิดพลาดออบเจ็กต์ 'numpy.ndarray' โดยไม่มีแอตทริบิวต์ "ผนวก" ฉันต้องการเพิ่มข้อมูลผู้ใช้ในการทดสอบอาร์เรย์ 11 ทำงานได้ดีโดยไม่ต้องทำ test11 เป็นอาร์เรย์ numpy แต่ฉันอยากจะ จำกัด ขนาดของตัวเลขไว้ที่ 20 ได้ไหม กรุณาทำให้มันง่าย
รหัสข้อผิดพลาด: Traceback (การโทรล่าสุดล่าสุด): บรรทัดที่ 10 ใน test11.append (data) AttributeError: วัตถุ 'numpy.ndarray' ไม่มีแอตทริบิวต์ 'ผนวก'
คำตอบ
ตะเข็บนี้จะทำงาน ฉันเปลี่ยนสองสามบรรทัดอีกครั้งและทำเครื่องหมายไว้
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)
รายการเวอร์ชัน
# 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)
นี่เป็นสิ่งที่กระชับที่สุดเท่าที่ฉันจะทำได้โดยไม่ต้องเปลี่ยนทั้งสิ่งและทำให้มันคล้ายกับสิ่งที่คุณเริ่มต้น
ตอนนี้ผู้ใช้ไม่สามารถใช้ตัวเลขเกิน 20 ตัวหรือตัวอักษรใด ๆ ไม่เช่นนั้นข้อผิดพลาดจะปรากฏขึ้น
อาร์เรย์ Numpy ไม่มีเมธอด 'ต่อท้าย' พวกเขาควรจะสร้างด้วยรูปร่างและความยาวที่คุณต้องการ ดังนั้นในกรณีของคุณวิธีที่ดีที่สุดคือสร้างรายการต่อท้ายค่าจากนั้นสร้างอาร์เรย์:
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)
คุณสามารถแทนที่คุณด้วยnp.append
:
np.append(test11,data)
แต่การต่อท้ายอาร์เรย์ numpy นั้นเสียค่าใช้จ่ายมากกว่าการต่อท้ายรายการ ฉันขอแนะนำให้ใช้list
โครงสร้างกับappend
และในตอนท้ายให้แปลงรายการของคุณเป็นอาร์เรย์ numpy โดยใช้np.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)
total = test11.sum()
print("The sum of all", total)