Chapter 3, part 2 Flashcards
Give examples of an if statement in pseudocode
- If student’s grade is greater than or equal to 60, display ‘Passed’
- If the condition is true, ‘Passed” is displayed. Then, the next pseudocode statement in order is ‘performed’
- If the condition is false, nothing is displayed, and the next pseudocode statement is ‘performed’
- Indentation emphasizes that ‘Passed’ is displayed only if the condition is true
Python code requires indentation. Here it emphasizes that ‘Passed’ is displayed only if the condition is true
What is suite indentation?
Indenting a suite is required; otherwise an IndentationError syntax error occurs
Must indent a total of 4 spaces when writing print after the if statement
T/F
If you indent a suite’s statements, you will not get an IndentationError
False. All the statements in a suite must have the same indentation. Otherwise an IndentationError occurs
What should you be mindful of when using the = sign?
Using the equality operator == instead of the assignment symbol = in an assignment statement can lead to subtle problems
== is used when assessing if something is true or false. Can be used in the same manner as is except is compares identities(if something is identical) while == compares if things are equal (doesnt have to be identical but has to have the same components)
= is used to assign a meaning to something or define it
What does adding an if… else statement do that an if statement doesn’t?
- Performs different suites, based on whether a condition is true or false
- Pseudocode:
If student’s grade is greater than or equal to 60
…..Display ‘Passed’
Else
…..Display ‘Failed’
Ignore the periods and pretend it’s indented
We indent both If and Else suites, and by the same amount
What are conditional statements?
When an if…else statment assign different values to a variable, based on a condition, as in:
if grade >= 60:
…..result = ‘Passed’
else:
…..resule = ‘Failed’
We can then print or evaluate that variable:
result
..’Passed’
Ignore the periods and pretend they are indented
The parenthesis are not required but they make it clear that the statement assigns the conditional expression’s value to result
Can write statements like this using a concise conditional expression
What would it look like on interactive mode? Use same example of:
if grade >= 60:
…..result = ‘Passed’
else:
…..result = ‘Failed’
Not shown but the second lines should have indentations
Ignore the periods and pretend it’s indented
‘Passed’ if grade >=60 else ‘Failed’
What would it look like to have multiple statements in a suite?
In [13]: grade = 49
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………………print(‘You must take this course again’)
Out[14] Failed
Pretend periods are indentations
In this case, grade is less than 60, so both statements in the else’s suite execute. If you do not indent the second print, then it’s not in the else’s suite. So, that statement always executes, creating strange incorrect output
What would it look like if we did not indent the second print on a multiple statement suite?
In [13]: grade = 100
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………….print(‘You must take this course again’)
Out[14] Passed
…………….You must take this course again
Ignore the periods and pretend they are indentations
What are if….elif……else statements?
They test for many cases and only the action for the first true condition executes
In [13]: grade = 77
In [14] if grade >= 90:
………………print(‘A’)
………….elif grade >= 80:
………………print(‘B’)
………….elif grade >= 70:
………………print(‘C’)
………….elif grade >= 60:
………………print(‘D’)
………….else:
………………print(‘F’)
Out[14] C
else is optional here, without else, if no conditions are true, the program does not execute any of the statement’s suites
What is a nonfatal logic error?
An incorrectly indented code segment in snippet:
In [13]: grade = 100
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………….print(‘You must take this course again’)
Out[14] Passed
…………….You must take this course again
The code executes but it produces incorrect results
What is a fatal logic error?
An exception occurs such as a ZeroDibisionError from an attempt to divide by 0, so Python displays a traceback, then terminates the script. A fatal error in interactive mode terminates only the current snippet. Then IPython waits for your next input.
T/F
A fatal logic error causes a script to produce incorrect results, then continue executing
False. A fatal logic error causes a script to terminate
What is a while statement?
Allows you to repeat one or more actions while a condition remains true. Such a statement often is called loop
Give an example of a while statement
In [1]: product = 3
In [2]: while product <= 50:
…….. product = product * 3
In [3]: product
Out[3]: 81
This loop goes like this:
3 is less than 50, so we do 3 x 3 = 9
9 is less than 50, so 9 x 3 = 27
27 is less than 50, so 27 x 3 = 81 and now the loop stops because Python again tests the condition, which is finally false because product is now 81. The repetition now terminates.
If this while statement were part of a larger script, execution would continue with the next statement in sequence after the while.