Thursday, February 9, 2017

Exercises - Conditional Execution

Exercise 1 – Write a program that give the employee 1.5 times the hourly rate for hours worked above 40 hours.
hours = raw_input("Enter hours: ")
payrate = raw_input("Enter rate: ")
h=float(hours)
br=float(payrate)
if h>40:
    extra = h-40
    base = h-extra
    sallary = (base*br)+(extra*br*1.5)
else:
    extra = 0
    base=0
    sallary = h*br
print "Pay: " + str(sallary)

Exercise 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 = raw_input("Enter hours: ")
payrate = raw_input("Enter rate: ")
try:
    h=float(hours)
    br=float(payrate)
    if h>40:
        extra = h-40
        base = h-extra
        sallary = (base*br)+(extra*br*1.5)
        print "Pay: " + str(sallary)
    else:
        extra = 0
        base=0
        sallary = h*br
        print "Pay: " + str(sallary)
except:
    print 'Error, please enter numeric input'  

Exercise 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 between 0.0 and 1.0, print a grade using the following table:

uservalue=raw_input("Enter score: ")
try:
    x = float(uservalue)
    if x<0 or="" x="">1.0:
        print "Bad score"
    else:
        if x<=1.0 and x>=0.9:
            print "A"
        elif x<=0.89 and x>= 0.8:
            print "B"
        elif x<=0.79 and x>= 0.7:
            print "C"
        elif x<=0.69 and x>= 0.6:
            print "D"
        elif x<0 .6:="" o:p="">
            print "F"
        else:
            print "Bad score"
except:
    print "Bad score"






No comments:

Post a Comment