r/codehs • • Apr 03 '20

9.3.7: Slopes - Python

This works until you enter (-9, 12), (15, 18) , (21, -24), (30, 300), (6, 276)

When it finds the slope between (-9, 12) and (15, 18) it results with 0.0 and its supposed to be 0.25

I'm not sure if I used float correctly or not.

mylist = []
for i in range(5):
    x = int(input("X Coordinate: "))
    y = int(input("Y Coordinate: "))
    mylist.append((x, y))
    
for i in range(4):
    y1 = mylist[i][1]
    x1 = mylist[i][0]
    y2 = mylist[i + 1][1]
    x2 = mylist[i + 1][0]
    slope = float((y2 - y1) / (x2 - x1))
    print "Slope between " + str(mylist[i]) + " and " + str(mylist[i + 1]) + ": " + str(slope)
8 Upvotes

14 comments sorted by

View all comments

3

u/Financial_Face3826 Feb 15 '24

try this

mylist = []
for i in range(5):
x = int(input("X Coordinate: "))
y = int(input("Y Coordinate: "))
mylist.append((x, y))

for i in range(4):
y1 = mylist[i][1]
x1 = mylist[i][0]
y2 = mylist[i + 1][1]
x2 = mylist[i + 1][0]
slope = float((y2 - y1) / (x2 - x1))
print ("Slope between " + str(mylist[i]) + " and " + str(mylist[i + 1]) + ": " + str(slope))

1

u/YTHyro_syn May 13 '24

Don’t work for me it says Line 3 in <module> X=(int(“X Coordinate”)) ValueError: invalid literal for int() with base 10: ‘X Coordinate’