02. Control Flow with Decisions and Loops 1 Flashcards

1
Q

Explain what will happen with the following code example.

annualSales = 300000
if annualSales >= 500000:
print(“Gold Customer”)
print(“Thank you for your business”)

A
  1. The if statement will evaluate to False and skip the indented code.
  2. The final print statement will be displayed onscreen.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When using if statements and wish to check for several conditions, what statement is required?

A
  1. The elif statement. This is short for else if.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does order matter when using if and elif statements?

A

TBC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain what purpose an else statement serves when using if statements.

A
  1. The else statement is known as the “If all else fails option”.
  2. It will execute if no if or elif statement is True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain what a nested if statement is?

A

It is an if statement inside of another if statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the variable type and value of newCustomer in line 8?

A
  1. It is a Boolean variable with a value of True
  2. Referring to a variable by itself is the same as giving it a Boolean value of True.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In simple terms, what does a for loop do?

A

A for loop is set to run a certain number of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many times will this for loop run for?

for monthlyGoal in range(12):
print(“Good job!”)

A

12 times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Coding exercise:

  1. Write a program to ask a user “What is the capital of Ireland?”
  2. If the user guesses incorrectly three times, the program exits.
  3. Print a statement if the user guesses correctly.
  4. Use a while loop, if statement and break statement to complete the exercise.
A

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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why might you use a continue statement in a for loop?

A

It is possible that you may wish to skip a certain iteration if a condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the pass keyword used for?

A

Think of the pass keyword as a placeholder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly