Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 4.6:
- Rewrite your pay computation with time-and-a-half for overtime and create a
- function called computepay which takes two parameters (hours and rate).
- Enter Hours: 45
- Enter Rate: 10
- Pay: 475.0
- """
- # Functions
- def computepay(hours, rate):
- """
- Computes the payment from hours * rate, above 40h: hours * rate * 1.5
- Returns pay
- """
- if float(hours) <= 40:
- pay = round(float(hours) * float(rate), 2)
- else:
- pay = round(40.0 * float(rate)
- + (float(hours) - 40) * (float(rate) * 1.5), 2)
- # 40 hours with normal rate, rest with rate * 1.5
- return pay
- def value_input():
- """
- Lets the user input 'hours' and 'rate'
- Returns hours, rate
- """
- hours = None
- rate = None
- # Hours input
- while hours == None:
- try:
- hours = float(input("Enter hours: "))
- except ValueError:
- print("Invalid input!")
- continue
- except:
- print("Error!")
- continue
- if hours < 0:
- print("Please enter a positive value!")
- hours = None
- # Rate input
- while rate == None:
- try:
- rate = float(input("Enter rate: "))
- except ValueError:
- print("Invalid input!")
- continue
- except:
- print("Error!")
- continue
- if rate < 0:
- print("Please enter a positive value!")
- rate = None
- return hours, rate
- # Main
- amount = None # The amount of data records (loops to compute the payment)
- # Amount input
- while amount == None:
- try:
- amount = int(input("Enter amount: "))
- except ValueError:
- print("Invalid input!")
- continue
- except:
- print("Error!")
- continue
- if amount < 0:
- print("Please enter a positive value!")
- amount = None
- for i in range(0, amount):
- entry = value_input()
- pay = computepay(entry[0], entry[1])
- print("Pay: " + str(pay) + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement