-3

I am trying to append or extend values by using "def" function, but, I am getting an error numpy.float64 object is not iterable

Basically, I want to store different slopes values in the variable name "all_slope" by using extend or append function. I am passing four different values in call function that is a slope. Would, it possible to help me?

all_slope=[]
def slope(x1,x2,y1,y2):
    x=x2-x1
    y=y2-y1
    slope_value=(y/x)
    all_slope.extend(slope_value)
    return all_slope
slope(3,2,4,2)
skaul05
  • 2,154
  • 3
  • 16
  • 26
sahaj patel
  • 131
  • 9

2 Answers2

2

Use append instead of extend:

Why:

extend: Extends list by appending elements from the iterable.

append: Appends object at the end.

Read more..

Hence:

all_slope=[]
def slope(x1,x2,y1,y2):
    x=x2-x1
    y=y2-y1
    slope_value=(y/x)
    all_slope.append(slope_value)
    return all_slope

print(slope(3,2,4,2))

OUTPUT:

[2.0]

EDIT:

Good catch by @ mfitzp, Since all_slope is a global var, you could just call the function and then print the list without return:

all_slope=[]
def slope(x1,x2,y1,y2):
    x=x2-x1
    y=y2-y1
    slope_value=(y/x)
    all_slope.append(slope_value)

slope(3,2,4,2)
print(all_slope)
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • @sahajpatel if the answer helped, you may click on the tick sign beside it to accept it. Cheers – DirtyBit Feb 19 '19 at 15:52
  • Sorry, I can't . Because I am a new user. I need to gain more reputation for that. – sahaj patel Feb 19 '19 at 18:58
  • @sahajpatel you can always accept an answer after 15 minutes of asking a question: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – DirtyBit Feb 20 '19 at 06:20
0

change all_slope.extend(slope_value) to all_slope.append(slope_value)

Alb
  • 1,063
  • 9
  • 33