
Python 101 - Lesson 3 - Chapter 3
By:
evita_gr on
Oct 10th, 2011 | syntax:
Python | size: 1.50 KB | hits: 57 | expires: Never
# Exercise 3.1 :
# Rewrite your pay computation
# to give the employee 1.5 times
# the hourly rate for hours worked
# above 40 hours.
hours_1 = int(raw_input('Enter Hours:'))
rate_1 = int(raw_input('Enter Rate:'))
if hours_1 <= 40:
pay = hours_1 * rate_1
elif hours_1 > 40:
hours_2 = hours_1 - 40
pay = 40 * rate_1 + hours_2 * 15
print 'Pay:',pay
# 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.
hours_1 = raw_input('Enter Hours:')
try:
hours = int(hours_1)
rate_1 = raw_input('Enter Rate:')
try:
rate = int(rate_1)
if hours <= 40:
pay = hours * rate
elif hours > 40:
hours2 = hours - 40
pay = 40 * rate + hours2 * 15
print 'Pay:', pay
except:
print 'Error, please enter numeric input'
except:
print 'Error, please enter numeric input'
# Exercise 3.3:
# Write a program to prompt for a score between
# 0.0 and 1.0. If the score is out of range print
# an error. If the score is between 0.0 and 1.0,
# print a grade using a table.
input_b = raw_input('Enter score:')
try:
input = float(input_b)
if input >= 0.0 and input < 1.0:
if input >= 0.9:
print 'A'
elif input >= 0.8:
print 'B'
elif input >= 0.7:
print 'C'
elif input >= 0.6:
print 'D'
else:
print 'F'
else:
print 'Bad score'
except:
print 'Bad score'