
3-2
By:
Mars83 on
Oct 3rd, 2011 | syntax:
Python | size: 1.17 KB | hits: 60 | expires: Never
#! /usr/bin/env python3.2
# -*- coding: utf-8 -*-
# main.py
""" Task: Exercise 3.2
Rewrite your pay program using try and except so that your program handles
non-numeric input gracefully by printing a message and exiting the program.
The following shows two executions of the program:
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Enter Hours: forty
Error, please enter numeric input
"""
# Main
try:
hours = input("Enter Hours: ")
hours = float(hours)
except:
print("Error, please enter numeric input!")
hours = -1 # Error indicator
if hours >= 0:
try:
rate = input("Enter Rate: ")
rate = float(rate)
except:
print("Error, please enter numeric input!")
rate = -1 # Error indicator
if hours < 0 or rate < 0: # No negative hours / rate possible
pass
elif hours <= 40:
pay = round(float(hours) * float(rate), 2)
print("Pay: " + str(pay))
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
print("Pay: " + str(pay))