Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Script: week_4 worksheet
- # Author: Scott Frasier
- # Purpose: Course work Programming for Investigators.
- '''
- 2. Write a program that converts feet to inches, only positive value of feet is
- accepted. (Hint: 1 feet = 12 inches)
- '''
- def ask_user_for_feet():
- while True: # Loop until break
- feet = raw_input("How many feet?: ") # Get feet from user
- try:
- feet = float(feet) # try to convert feet to a float
- if feet < 0: # check to make sure value is positive
- raise ValueError # if not raise error and re-loop
- break # if in converts then break from the while loop
- except: # if feet is not a float or integer tell the user and re-loop
- print("feet must be a positive integer or float!")
- return feet
- def main():
- print("Enter the number of feet you want converted to inches: ")
- feet = ask_user_for_feet()
- inches = feet * 12.0
- # using .format is more readable and the preferred method in the python
- # documentation
- message = "{} feet converts to {} inches."
- print(message.format(feet, inches))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement