day06notes

py

School

Georgia Institute Of Technology *

*We aren’t endorsed by this school

Course

1301

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

4

Uploaded by BrigadierBravery5404

Report
""" Day 06 - More conditionals 1. Recap 2. More Boolean Practice a. Evaluating Boolean Expressions b. Using Boolean Expressions in Conditionals c. Returning a Boolean from a Function d. Using Functions in Conditionals 3. Advanced Conditionals a. Nested if statements 4. The "in" operator a. Using "in" with strings """ # >>> Day 06 Miniquiz - Problem solving # === Recap: Conditionals # booleans ''' val1 = 78 val2 = 89 comparison = val1 >= val2 print(comparison) ''' # if/elif/else ''' val4 = 100 val3 = 10 if val3 < 80: print("first branch of conditional!") elif val4 > 50: print("second branch of conditional!") else: print("Nothing was satisfied!") ''' # if/if vs if/elif ''' val4=45 val3=100 if val4 > 20: print("Hey!") elif val3 > 90: print("Hello!") val4=45 val3=100 if val4 > 20: print("Bye!") if val3 > 90: print("Buh-bye!") ''' # === Conditionals in functions # Example: Write a function that takes as input someone's # grades on this course and returns their letter grade. # HINT: You might need to go to the syllabus to find the
# weights for exams, homeworks, labs, participation and # the final. ''' def letterGrade(mid, hw, quiz, final, lab): total = 0.4 * mid + 0.25 * hw + 0.05 * quiz + 0.2 * final + 0.1* lab print(total) if total >=90: return "A" elif total >=80: return "B" elif total >=70: return "C" elif total >=60: return "D" else: return "F" student1= letterGrade (90, 100, 100, 80, 100) print(student1) ''' # === Functions that return booleans # Example: Write a function that checks whether a number is # divisible by 4. The function should return the result # as a bool. ''' def isDivBy4(num): remainder = num % 4 if remainder == 0: return True else: return False print(isDivBy4(16)) def oneLiner(num): return num%4 == 0 ''' # Example: Write a function that checks whether a password # is correct or not. The function should return the result # as a bool. ''' def isCorrectPwd(pwd): databasePwd = "helloKitty500" if pwd == databasePwd: return True else: return False ''' ''' print(isCorrectPwd("HelloKitty900")) ''' # === Logical operators (not, and, or) # not ''' var23 = True
print(not var23) var24 = False print(not var24) ''' # and var0 = 100 var3 = 90 var7 = 54 ''' if var0 >80 and var3 < 100: print("Hey") # if True and True # if True if var0 >80 and var3 > 100: print("Bye") # if True and False # if False if var0 <80 and var7 > 100: print("Bye") # if False and False # if False ''' # or ''' if var0 > 80 or var7 > 100: ''' #if True or False #if True # using them all at once #7<8 and not 6==0 or 100<23: #if True and not False or False #if True and True or False #if True or False #if True # Example: Write a function that checks whether a number is # in the range of 0 to 15 or in the range of -5 and -2. # INCLUSIVE!! ''' def range(num): if num >= 0 and num <=15 or num >= -5 and num <= -2: return True else: return False print(range(3)) print(range(20)) print(range(-3)) ''' # === Nested if statements # Example: Writing a function that tells us where to have # lunch today based on two input parameters: cheap and fast # The function should print the name of the restaurant that # matches the combinations of those values.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# Example: Is it a good day to go hiking? # Temperature | Weather | Hike? # T > 80 | rainy | yes # T > 80 | clear | no # 50 <= T | clear | yes # 50 <= T | rainy | no def weShouldHike(temperature, weather): if temperature > 80: if weather == "rainy": return True elif weather == "clear": return False elif temperature < 50: if weather == "rainy": return False elif weather == "clear": return True print(weShouldHike(85, "rainy")) # >>> Day 06 Miniquiz - Advanced conditionals # === (If we have time today!) The in operator # Follow-up example: Write a function that checks whether a # breed of dog is friendly. The function should return the # result as a bool. # Example: Write a function that prints a targeted ad based # on the contents of a message.