Exercise 2
# input a name and print it with a greeting
name = raw_input("Enter your name")
print "Howdy", name;
Exercise 3
#An hours worked times rate program
#Introduces input, float and operator expression
inp = raw_input("Enter Hours: ")
hours = float(inp)
inp = raw_input("Enter Rate: ")
rate = float(inp)
#print rate, hours
pay = rate * hours
print "Pay in Dollars is: ", pay
Conditional Execution
Exercise 1
#An hours worked times rate program that checks for overtime
#Introduces conditional statements
inp=raw_input("Enter Hours:")
hours = float(inp)
inp=raw_input("Enter Rate:")
rate=float(inp)
if hours<=40:
pay = rate*40
else:
pay = rate*40 + ((hours-40)*1.5*rate)
print pay
Exercise 2
#An hours worked times rate program that checks for overtime
#Introduces a "try" and "except" routine to minimize program crashes and simply quit
try:
inp=raw_input("Enter Hours:")
hours = float(inp)
inp=raw_input("Enter Rate:")
rate=float(inp)
except:
print "Error, please enter numeric input"
quit()
if hours<=40:
pay = rate * hours
else:
pay = rate * 40 + ((hours-40) * 1.5 * rate)
print pay
Exercise 3
# Calculate letter grade from numeric
# Introduce conditional if/else
try:
inp=raw_input("Enter Numeric Score of Grade:")
score = float(inp)
except:
print "Error, please enter numeric input"
quit()
if score>=0.9 :
grade = "A"
print grade
elif score >=0.8 :
grade = "B"
elif score>=0.7 :
grade = "C"
elif score >=0.6 :
grade = "D"
else :
score <0.6
grade = "F"
print grade
Functions
Exercise 6
#An hours worked times rate program that checks for overtime
#Introduces a "try" and "except" routine to minimize program crashes and simply quit
#Introduces a function via "def"
def computepay(h,r):
if h<=40:
p = r*h
else:
p = r*40 + ((h-40)*1.5*r)
return p
try:
inp=raw_input("Enter Hours:")
hours = float(inp)
inp=raw_input("Enter Rate:")
rate=float(inp)
except:
print "Error, please enter numeric input"
quit()
pay=computepay(hours,rate)
print pay
Loops and Iterations
Exercise 1
# Input data until "done"
# Compute an average
# Introduce iterations
count = 0
total = 0
num = 0
while True:
inp = raw_input('Enter a number: ')
if inp == 'done': break
if len(inp) <1: break #check for empty line
try:
num = float(inp)
except:
print "Invalid input"
continue
count = count + 1
total = total + num
print num, total, count
print 'Average: ', total/count
Strings
Exercise 5
# Take a string and strip to get the far right numeric
text = "X-DSPAM-Confidence: 0.8475";
pos = text.find(':')
num = float(text[pos+1:])
print num
Lists
Exercise
# Read a list of words
# Alphabetically sort and print
words = ['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks']
words.sort()
print words