Skip to content

Instantly share code, notes, and snippets.

@Tyaedalis
Created October 10, 2012 03:26
Show Gist options
  • Save Tyaedalis/3863003 to your computer and use it in GitHub Desktop.
Save Tyaedalis/3863003 to your computer and use it in GitHub Desktop.
from fractions import Fraction
def strToNum(string):
if '/' in string: #if user entered a fraction
string = string.split('/')
return Fraction(int(string[0]), int(string[1]))
elif '.' in string: #if user entered a decimal
return float(string)
else: return int(string)
run = 1
while run:
coords = []
for x in range(2):
coords.append(input("coord {}: ".format(x+1)).split())
coords = [(strToNum(x), strToNum(y)) for (x, y) in coords]
x1, y1 = coords[0][0], coords[0][1]
x2, y2 = coords[-1][0], coords[-1][1]
delta_y = y2 - y1
delta_x = x2 - x1
print()
print("∆y = {} - {} = {}".format(y2, y1, delta_y))
print("∆x = {} - {} = {}".format(x2, x1, delta_x))
if type(delta_y) != 'Fraction' and type(delta_x) != 'Fraction':
print("m = {}/{} ≈ {}".format(delta_y, delta_x, float(delta_y/delta_x)))
else:
print("m = {} ≈ {}".format(delta_y/delta_x, float(delta_y/delta_x)))
print()
cont = input("Another? (y/n): ")
if cont == 'n':
run = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment