#!/usr/bin/env python # coding: utf-8 # In[7]: # Section 4.6 -- input validation loops # first a simple program (with no input validation) for gross pay # get hours worked from user hours_worked=float(input("How many hours did you work this week?")) # get pay rate pay_rate=float(input("What is your hourly pay rate?")) # calculate the gross pay gross_pay=hours_worked*pay_rate print("Your gross pay is $",format(gross_pay,'.2f'),sep='') # In[11]: # Do the same program with input validation # We will assume inputs are floats for now # (i.e. we won't check to see if a string is inputted) # Criteria: any nonpositive pay rate is bad and # get hours worked from user must be >0 and <100 # in other words: need to check that # hours_worked>0 and hours_worked<100 and pay_rate>0 hours_worked=float(input("How many hours did you work this week?")) # get pay rate pay_rate=float(input("What is your hourly pay rate?")) while not hours_worked>0 or not hours_worked<100 or not pay_rate>0: print("There was an error in your input. Please try again.") hours_worked=float(input("How many hours did you work this week?")) pay_rate=float(input("What is your hourly pay rate?")) # calculate the gross pay gross_pay=hours_worked*pay_rate print("Your gross pay is $",format(gross_pay,'.2f'),sep='') # In[20]: # updated password checker program # if the password wrong, ask again # if wrong 3 times, give an error and stop the program # # set the secret password secret_password='zugzwang' # set counter to track how many times the user inserted a wrong pw counter=0 # get password from user inserted_password=input("What is the secret password?") while counter<4 and inserted_password != secret_password: print("Error -- incorrect password. Please try again.") counter+=1 inserted_password=input("What is the secret password?") if counter==4: print("You have made too many attempts to log in. Please try again later. The admin has been notified.") else: print("You have correctly inserted the secret password.") # In[23]: # Section 4.7 -- nested loops # loops in loops # Example program -- let's simulate a digital clock # see diagram on board for hour in range(24): for minute in range(60): for second in range(60): print(format(hour,'02d'),":",format(minute,'02d'),":",format(second,'02d'),sep='')