#!/usr/bin/env python # coding: utf-8 # In[17]: # Section 3.1 - "if statements" # # an if statement checks if something holds # 1. if the something holds, then it does something # 2. if the something doesn't hold, then it doesn't do anything # # # Syntax # if condition: # do a thing # do another thing # bla bla bla # code here, is not part of the "do a thing" portion # # Example from board: # # if it is cold: # put on a jacket # go outside # # "Relational operators" # Symbol Meaning # > greater than # < less than # >= greater than or equal to # <= less than or equal to # == equality (as in math class) # != not equal (as in math class) # Example -- the following code gives an error because "x=5" # is NOT the "relational operator" for checking # equality of two statements # "=" is variable assignment, "==" is to check equality #x=5 #if x=5: # print("x is five!!!!") # # Expression Meaning # x > y "Is x greater than y?" # x < y "Is y less than x?" # x <= y "Is x less than or equal to y?" # x >= y "Is x greater than or equal to y?" # x==y "Is x equal (mathematically) to y?" # x!=y "Is x NOT equal (mathematically) to y?" # # # Test these operations x=1 y=0 print("We set x=1 and y=0") print("x>=y is",x>=y) # this gives "True" as an output print("x==y is",x==y) # this gives "False" print("x!=y is",x!=y) # this gives "True" z=1 print("Now we set z=1") print("x==z is",x==z) # Notice something interesting: print("The type of x>y is",type(x>y)) # We got that the type was "bool". This happens for any relational operator: print("The type of x!=y is",type(x!=y)) print("The type of x==y is",type(x==y)) # bool is short of "boolean" which is named after logician # George Boole who investigated "boolean algebra" in the 1800s among other things # # # # In[33]: # Write a program to take three test scores, report the average, and then # give a response to the user related to the value of the average. # # Maybe: if average < 60 tell the user "Your grade is not so good." # if average > 90 tell the user "Your grade is great!" # The 60 and the 90 are magic numbers... we should define them with named constants # # Alteration from a class question: can we give a message if the score # is between 60 and 90? LOW_AVG=60 HIGH_AVG=90 # obtain three test scores from the user: score1=float(input("What was the grade on exam 1?")) score2=float(input("What was the grade on exam 2?")) score3=float(input("What was the grade on exam 3?")) # compute the average average=(score1+score2+score3)/3 print("Your average score was",average,"percent.") # make further responses for extreme scores if average < LOW_AVG: print("Your grade is not so good.") if average > HIGH_AVG: print("Your grade is great!") ##### #extra code from the question in class ##### ##### if average >= LOW_AVG: if average<=HIGH_AVG: print("Your grade is... ok.") # note -- this can be done in one line with a "logical operator" # "AND" which would be written as if average >= LOW_AVG and average <= HIGH_AVG: ##### ##### print("Please record your score for future use.") # ###