Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Script: week_4 worksheet
  3. # Author: Scott Frasier
  4. # Purpose: Course work Programming for Investigators.
  5.  
  6. '''
  7. 2. Write a program that converts feet to inches, only positive value of feet is
  8. accepted. (Hint: 1 feet = 12 inches)
  9. '''
  10.  
  11.  
  12. def ask_user_for_feet():
  13. while True: # Loop until break
  14. feet = raw_input("How many feet?: ") # Get feet from user
  15. try:
  16. feet = float(feet) # try to convert feet to a float
  17. if feet < 0: # check to make sure value is positive
  18. raise ValueError # if not raise error and re-loop
  19. break # if in converts then break from the while loop
  20. except: # if feet is not a float or integer tell the user and re-loop
  21. print("feet must be a positive integer or float!")
  22. return feet
  23.  
  24.  
  25. def main():
  26. print("Enter the number of feet you want converted to inches: ")
  27.  
  28. feet = ask_user_for_feet()
  29. inches = feet * 12.0
  30.  
  31. # using .format is more readable and the preferred method in the python
  32. # documentation
  33. message = "{} feet converts to {} inches."
  34. print(message.format(feet, inches))
  35.  
  36. if __name__ == "__main__":
  37. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement