The Cost of Poor Test Coverage
- 0.3 - 0.2 == 0.1
- U.S. Details Flaw in Patriot Missle
Reading Questions
Quiz
Booleans
- The Boolean type has only 2 values,
True
andFalse
Boolean expressions evaluate to Boolean values
print(1 == 2) print(1 != 2) print(1 < 2) print(1 > 2) print(1 <= 2) print(1 >= 2)
Conditionals
- Write code that executes only if a condition is met
Boolean values are useful for specifying the conditions
integer = int(input('enter an integer: ')) if integer == 0: print('You entered Zero') else: print('You did not enter Zero')
- Can also leave off the else
This can be toublesome if a variable is created in the if block
x = 0 if x != 0: y = x print(y)
It is possible to put conditionals inside of conditionals
number = int(input('enter an integer: ')) if number < 10: if number > 0: print('you entered a number between 1 and 9')
- The print statement in the inner if statement is only executed if both the outer and the inner are true
- If either are false it is not executed
- If statements can also be inside of loops and loops can be inside of if statements
- Unsuprisingly they can also be inside of functions
Note, returns and if statments can produce unexpected results
def if_something(number): if number < 0: return number * -1 print('you entered non-negative number')
- The above function only have a return statement inside of the if
- So if a positive number is given as input, it will return None
- And it will not print if a negative number is entered
A good way to prevent these problems is to have just a single return statement as the final line of the function