#!/usr/bin/env python # coding: utf-8 # In[6]: # Section 3.2 -- if-else statements # if-else allows code to be run if a condition fails; this is different # then the behavior of an "if" statement because the "if" statement does # NOT do anything if the condition fails # # Syntax: # if condition: # stuff (when condition succeeds) # more stuff # else: # other stuff (when condition fails) # other other stuff # # # Write the example from the board in code: # the example on the board says to check if "temp < 40", if yes then # put on coat, if not then put on sandals # set the temperature that changes behavior CUTOFF_TEMP=40 # get the current temperature from the user current_temp=float(input("What is the current temperature?")) #implement the decision structure if current_temp < CUTOFF_TEMP: print("Put on your coat!") else: print("Put on your sandals!") # In[9]: # Overtime calculator #magic numbers OVERTIME_CUTOFF=40 PAYRATE_MULTIPLIER=1.5 # for overtime pay, what we multiply pay rate by to get the overtime pay rate #get hours worked and the pay rate from the user hours_worked=float(input("How many hours did you work?")) pay_rate=float(input("What is your hourly pay rate?")) # decision structure if(hours_worked > OVERTIME_CUTOFF): hours_of_overtime=hours_worked-OVERTIME_CUTOFF pay=OVERTIME_CUTOFF*pay_rate+hours_of_overtime*PAYRATE_MULTIPLIER*pay_rate else: pay=hours_worked*pay_rate # question was: how to implement tax? # answer: first calculuate "gross pay" as above, then apply taxes # if tax=3% you could say # tax=pay*0.03 # taxed_pay=pay-tax print("You earned $",pay,sep='') # In[14]: # Section 3.3 -- comparing strings # what happens if we try to compare strings with "<" and so on? string1='teach' string2='python' string3='teach' print("string1string2 yields",string1>string2) print("string1==string2 yields",string1==string2) print("string1==string3 yields",string1==string3) # In[17]: # Password check program #set the password PASSWORD='zugzwang' #get a password from the user inputted_password=input("What is the password?") # determine if the inputted password was correct or not if(inputted_password == PASSWORD): print("You got the right password. You may enter.") else: print("Incorrect password. Please try again.") # In[25]: # question: can we make everything lowercase? # test='xVtomvX' print(test.lower()) print(test) # Password check program (modified to remove case-sensitivity) #set the password PASSWORD='zuGzWang' #get a password from the user inputted_password=input("What is the password?") # determine if the inputted password was correct or not if(inputted_password.lower() == PASSWORD.lower()): print("You got the right password. You may enter.") else: print("Incorrect password. Please try again.") # In[28]: # this shows that actually ASCII codes are being compared, # so although we would normally say "Zlphabet" comes before # 'augzwang' in alphabetical order, the reality is that # the character 'Z' comes before the character 'a' in ASCII # code, so it is actually comparing those # # see a list of ASCII codes at https://en.wikipedia.org/wiki/ASCII under "Printable Characters" string1='Zlphabet' string2='augzwang' print('string1