#!/usr/bin/env python # coding: utf-8 # In[4]: # One more if-else example: loan qualification program # # If a person makes 30,000/yr AND also has been at their # job for >= 2 years, then they qualify for a special loan. # Otherwise they do not. # #magic numbers SALARY_CUTOFF=30000 YEARSWORKED_CUTOFF=2 #get info from the user salary=float(input("What is your annual salary?")) yearsatjob=float(input("How long have you been at your job?")) #first check the salary if salary >= SALARY_CUTOFF: #now we must check for years worked if yearsatjob >= 2: print("You do qualify.") else: print("You do not qualify.") else: print("You do not qualify.") # In[10]: #Section 3.4 - if, elif, else # # Syntax # if condition: # do stuff # elif another condition: # do stuff # elif another another condition: # do stuff # . # . # . # else: # stuff you do if all the others fail # # Grader program # Take from user their grade and report back their letter grade # 90<=grade<=100 --- A # 80<=grade<90 --- B # 70<=grade<80 --- C # 60<=grade<70 --- D # grade<60 --- F # magic numbers A_CUTOFF=90 B_CUTOFF=80 C_CUTOFF=70 D_CUTOFF=60 #get grade from user grade=float(input("What is your current grade?")) #decision structure: if grade>=A_CUTOFF: print("You got an A!") elif grade>=B_CUTOFF: print("You got a B!") elif grade>=C_CUTOFF: print("You got a C!") elif grade>=D_CUTOFF: print("You got a D!") else: print("You got an F!") # In[15]: #3.5 logical operators # idea: combine multiple conditional checks into one # # Operator Meaning # and both must be true # or at least one must be true # not reverses truth # # Examples x=1 y=2 a=3 b=4 print("xb is",xb) print("xb is",xb) print("xb is",xb) # In[18]: # Redo the loan program with logical operators #magic numbers SALARY_CUTOFF=30000 YEARSWORKED_CUTOFF=2 #get info from the user salary=float(input("What is your annual salary?")) yearsatjob=float(input("How long have you been at your job?")) if(salary >= SALARY_CUTOFF and yearsatjob >= YEARSWORKED_CUTOFF): print("You qualify!") else: print("You do not qualify.")