01. Control Flow with Decisions and Loops 1 Flashcards

1
Q

Explain what is happening in the following code sample.

name = “Alice”
if name = “Alice”:
print(“Hi Alice”)

A
  1. The string variable of name is set to the value of Alice
  2. If the if statement evaluates to True, then the following indented code is executed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is an else statement executed?

A

When the condition is False.

See the example below.

password = ‘Cisco’

if password =’Citrix’:
print(‘Access Granted’)
else:
print(‘Wrong password’)

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

Explain what is happening in the following code, specifically, the if statement.

print(‘Enter a name’)

name = input()

if name:
print(‘Thanks for entering a name’)
else:
print(‘You did not enter a name’)

A

A non-blank string value evaluates to True.

So if the user did enter a name, the first print statement would be executed.

The previous code example demonstrates “Truthy” or “Falsey” values.

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

What will the following code evaluate to?

bool(0)

A

False

0 = False

1 = True

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

Describe what is happening with the following while loop.

spam = 0
while spam < 5:
print(‘Hello World’)
spam += 1

A
  1. The integer value of spam begins at zero
  2. While the value of spam is less than five, or in other words, the condition is True, the indented code will continue to execute until the condition becomes False.
  3. The while loop will ‘iterate’ five times.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain what will happen with the following while loop.

name = “ “
while name != “your name”:
print(“Please enter your name”)
name = input()
print(“Thank you”)

A
  1. The name variable is set to a blank string.
  2. The while condition will evaluate to True as the name variable does not equal “your name”. Therefore, the indented code will be evaluated next.
  3. The user will be prompted to enter their name, and the next iteration will occur.
  4. If the user did not enter the string “your name”, the while loop will continue to prompt for a name.
  5. Only when the user enters “your name”, will the while loop evaluate to false and the final print statement will then be displayed, i.e. “Thank You”.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain what is happening with the following while loop.

while True:
print(“Hello”)

A
  1. The condition of the while loop will always evaluate to True and therefore, the program will print “Hello” each time.
  2. The loop will never evaluate to False, so it is an infinite loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the keyboard shortcut to end an infinite loop?

A

Ctrl+C

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

What purpose does a break statement serve when inserted into a while loop.

A

The break statement causes the execution to immediately jump out of the while loop and execute the next line of code after the while loop.

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

Explain what is happening in this while loop and specifically, the break statement.

name = “ “
while True:
print(“Please enter your name”)
name = input()
if name == “your name”:
break
print(“Thank You”)

A
  1. The while loop will always evaluate to True and prompt for user input.
  2. If the user enters “your name”, the break statement will cause the program to jump out of the loop and execute the final print statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What function does a continue statement serve when used within a while loop?

A
  1. When a program reaches a continue statement within a while loop, the execution immediately jumps back to the start of the while loop.
  2. The program then starts to reevaluate the loops condition.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain what is happening with the following while loop

spam = 0
while spam <5:
spam += 1
if spam == 3:
continue
print(‘Spam is ‘ + str(spam))

A
  1. The spam integer variable starts at zero
  2. While spam is less than five, in other words True.
  3. The value of spam will be incremented by one.
  4. If the value of spam is equal to three, the continue statement will cause the program to go back to the start of the while loop to reevaluate the condition.
  5. Therefore, the output will be…

Spam is 1
Spam is 2
Spam is 4
Spam is 5

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

What is the fundamental difference between a while loop and a for loop?

A
  1. A while loop will iterate while a condition is True.
  2. A for loop will iterate a specific amount of times.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain what is happening with the following for loop.

print(“My name is”)
for i in range(5):
print(“Jimmy Five Times “ + str(i))

A
  1. The first print statement is executed.
  2. The value of i is set to zero.
  3. The indented print statement is executed and the value of i is concatenated to the string.
  4. The for loop will repeat 4 more times until the range condition is met.
  5. Remember, it will loop up to, and not including 5.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Using a for loop, write a script to add up every number from 1 to 100. Print the final total only.

A

total = 0
for num in range(101):
total = total + num
print(total)

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

Explain what is happening in this for loop, specifically, the range function in the for loop.

print(“My name is: “)
for i in range(12, 16)
print(“Jimmy Five times “ + str(i))

A
  1. The for loop will begin at 12 and go up to, and not include 16.
  2. Therefore, it will print the following.

Jimmy Five Times 12
Jimmy Five Times 13
Jimmy Five Times 14
Jimmy Five Times 15

17
Q

The following for loop contains a step value. Explain what a step value is?

print(“My name is: “)
for i in range(0, 10, 2)
print(“Jimmy Five times “ + str(i))

A
  1. A step value is the amount the variable is increased after each iteration.
  2. In this example, it will increase by 2 each iteration.
  3. 0 is the start value and 10 is the end value. The for loop will go up to, and not include 10.
18
Q

The following for loop contains a step value. Explain what is happening with the step value this time?

print(“My name is: “)
for i in range(10, 1, -1)
print(“Jimmy Five times “ + str(i))

A
  1. This time the for loop is beginning at 10 and decreasing by 1 upon each iteration. It is counting backwards.