Advertisement
Mars83

3-2

Oct 3rd, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 3.2
  6.    Rewrite your pay program using try and except so that your program handles
  7.    non-numeric input gracefully by printing a message and exiting the program.
  8.    The following shows two executions of the program:
  9.    Enter Hours: 20
  10.    Enter Rate: nine
  11.    Error, please enter numeric input
  12.    Enter Hours: forty
  13.    Error, please enter numeric input
  14. """
  15.  
  16. # Main
  17. try:
  18.     hours = input("Enter Hours: ")
  19.     hours = float(hours)
  20. except:
  21.     print("Error, please enter numeric input!")
  22.     hours = -1  # Error indicator
  23. if hours >= 0:
  24.     try:
  25.         rate = input("Enter Rate: ")
  26.         rate = float(rate)
  27.     except:
  28.         print("Error, please enter numeric input!")
  29.         rate = -1   # Error indicator
  30. if hours < 0 or rate < 0:   # No negative hours / rate possible
  31.     pass
  32. elif hours <= 40:
  33.     pay = round(float(hours) * float(rate), 2)
  34.     print("Pay: " + str(pay))
  35. else:
  36.     pay = round(40.0 * float(rate)
  37.                 + (float(hours) - 40) * (float(rate) * 1.5), 2)
  38.                 # 40 hours with normal rate, rest with rate * 1.5
  39.     print("Pay: " + str(pay))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement