Advertisement
Mars83

4-6

Oct 4th, 2011
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 4.6:
  6.    Rewrite your pay computation with time-and-a-half for overtime and create a
  7.    function called computepay which takes two parameters (hours and rate).
  8.    Enter Hours: 45
  9.    Enter Rate: 10
  10.    Pay: 475.0
  11. """
  12.  
  13. # Functions
  14. def computepay(hours, rate):
  15.     """
  16.    Computes the payment from hours * rate, above 40h: hours * rate * 1.5
  17.    Returns pay
  18.    """  
  19.     if float(hours) <= 40:
  20.         pay = round(float(hours) * float(rate), 2)
  21.     else:
  22.         pay = round(40.0 * float(rate)
  23.                     + (float(hours) - 40) * (float(rate) * 1.5), 2)
  24.                     # 40 hours with normal rate, rest with rate * 1.5
  25.     return pay
  26.  
  27. def value_input():
  28.     """
  29.    Lets the user input 'hours' and 'rate'
  30.    Returns hours, rate
  31.    """
  32.     hours = None
  33.     rate = None
  34.     # Hours input
  35.     while hours == None:
  36.         try:
  37.             hours = float(input("Enter hours: "))
  38.         except ValueError:
  39.             print("Invalid input!")
  40.             continue
  41.         except:
  42.             print("Error!")
  43.             continue
  44.         if hours < 0:
  45.             print("Please enter a positive value!")
  46.             hours = None
  47.     # Rate input
  48.     while rate == None:
  49.         try:
  50.             rate = float(input("Enter rate: "))
  51.         except ValueError:
  52.             print("Invalid input!")
  53.             continue
  54.         except:
  55.             print("Error!")
  56.             continue
  57.         if rate < 0:
  58.             print("Please enter a positive value!")
  59.             rate = None
  60.    
  61.     return hours, rate
  62.  
  63. # Main
  64. amount = None   # The amount of data records (loops to compute the payment)
  65. # Amount input
  66. while amount == None:
  67.     try:
  68.         amount = int(input("Enter amount: "))
  69.     except ValueError:
  70.         print("Invalid input!")
  71.         continue
  72.     except:
  73.         print("Error!")
  74.         continue
  75.     if amount < 0:
  76.         print("Please enter a positive value!")
  77.         amount = None
  78.  
  79. for i in range(0, amount):
  80.     entry = value_input()
  81.     pay = computepay(entry[0], entry[1])
  82.     print("Pay: " + str(pay) + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement