02. Control Flow with Decisions and Loops 1 Flashcards
Explain what will happen with the following code example.
annualSales = 300000
if annualSales >= 500000:
print(“Gold Customer”)
print(“Thank you for your business”)
- The if statement will evaluate to False and skip the indented code.
- The final print statement will be displayed onscreen.
When using if statements and wish to check for several conditions, what statement is required?
- The elif statement. This is short for else if.
Does order matter when using if and elif statements?
TBC
Explain what purpose an else statement serves when using if statements.
- The else statement is known as the “If all else fails option”.
- It will execute if no if or elif statement is True
Explain what a nested if statement is?
It is an if statement inside of another if statement.
What is the variable type and value of newCustomer in line 8?
- It is a Boolean variable with a value of True
- Referring to a variable by itself is the same as giving it a Boolean value of True.
In simple terms, what does a for loop do?
A for loop is set to run a certain number of times.
How many times will this for loop run for?
for monthlyGoal in range(12):
print(“Good job!”)
12 times
Coding exercise:
- Write a program to ask a user “What is the capital of Ireland?”
- If the user guesses incorrectly three times, the program exits.
- Print a statement if the user guesses correctly.
- Use a while loop, if statement and break statement to complete the exercise.
Note: The indentation may not be 100% correct.
answer = str(input('What is the capital of Ireland? ')).lower() guesses = 1
while answer != ‘dublin’:
guesses +=1
if guesses > 3:
print(“You guessed incorrectly three times. Game over!”)
break
answer = str(input(‘Guess again: ‘)).lower()
if guesses <=3:
print(‘That is correct’)
Why might you use a continue statement in a for loop?
It is possible that you may wish to skip a certain iteration if a condition is met.
What is the pass keyword used for?
Think of the pass keyword as a placeholder.