I really need help. The output is supposed to read a text file with the coordinates and print them to a text file, but I am not sure what lines of code to write and where to insert them.

Here are the coordinates that are in there own text file:
1,16,10,31
2,12,21,9
3,7,8,25
4,5,15,3
5,1,6,3

def findSlope (x1, y1, x2, y2):
    rise = y2 - y1
    run = x2 - x1
    if run == 0:
        slope = "undefined"
    else:
        slope = rise/run
    return slope

import sys, string

inFile = open("C:\Python\coordinates.txt", "r")
outFile = open("output.txt", "w")

for i in inFile:    
    origin = i.split(',')
    j=0
    for j in range(len(origin)):
        origin[j] = str.strip(str(origin[j]), "\n")

    inFile2 =  open("C:\Python\coordinates1.txt", "r")
       
    for k in inFile2:
        if k!=i:
            destination = k.split(',')
            e=0
            for e in range(len(destination)):
                destination[e] = str.strip(str(destination[e]), "\n")
                print origin
                print destination
                print findSlope(int(origin[1]), int(destination[1]), int(origin[2]), int(destination[2]))
    inFile2.close()
    outFile2.close()
                
inFile.close()
outFile.close()

Frist, you should test the findSlope function (see the python style guide. Function names are all lower case with underscores and you should not use "i", "l", or "O" as single digit variable names because they can look like numbers).

def findSlope (x1, y1, x2, y2):
    rise = y2 - y1
    run = x2 - x1
    if run == 0:
        slope = "undefined"
    else:
        slope = rise/run
    print "slope is", slope, rise, run
    return slope

findSlope(1,16,10,31)

yields a slope of 1 because you are only using integers.

def findSlope (x1, y1, x2, y2):
    rise = y2 - y1
    run = x2 - x1
    if run == 0:
        slope = "undefined"
    else:
        slope = rise/float(run)
    print "slope is", slope, rise, run
    return slope

findSlope(1,16,10,31)

yields a slope of 1.6666... --> converting to a float.

For reading the file, try:

for rec in inFile:    
    print"-----  new record -----"
    rec = rec.strip()
    rec_list = rec.split(',')
    ctr = 0
    for el in rec_list:
        print ctr, el, type(el)   ## is a string, not an integer or float
        ctr += 1

Also, you read 2 different files, but only explain what one of them is???

Edited 6 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.