Advertisement
J2112O

Python Function Problems

Jul 8th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # This program will contain two functions, slope and intercept                                                        
  2.   2 # slope_intercept.py                                                            
  3.   3                                                                                
  4.   4 import math                                                                    
  5.   5                                                                                
  6.   6 def slope(x1,y1,x2,y2):                                                        
  7.   7     """This function finds the slope of a line (int or float format)"""        
  8.   8     rise = (y2 - y1)                                                            
  9.   9     run = (x2 - x1)                                                            
  10.  10     slope = (rise / run)                                                        
  11.  11     if type(slope) == float: #checks to see if numbers are int or float        
  12.  12         return round(slope,2)                                                  
  13.  13     else:                                                                      
  14.  14         return slope                                                            
  15.  15                                                                                
  16.  16 def intercept(x1,y1,slope):                                                    
  17.  17     """This function figures the y-intercept of a line"""                      
  18.  18     a = slope * x1                                                              
  19.  19     intercept = y1 - a                                                          
  20.  20     return intercept                                                            
  21.  21                                                                                
  22.  22 def main():                                                                    
  23.  23                                                                                
  24.  24     print("Enter the first set of point coordinates separated by a comma. ")    
  25.  25     x1,y1 = input()                                                            
  26.  26     print("Enter the second set of point coordinates separated by a comma. ")  
  27.  27     x2,y2 = input()                                                            
  28.  28     print("The slope of those coordinates is: ") , slope(x1,y1,x2,y2)
  29.  29     print("The y-intercept of that line is: ") , intercept(x1,y1,slope)        
  30.  ,30                                                                                                                      
  31.  31                                                                                
  32.  32 if __name__ == "__main__":                                                      
  33.  33     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement