01. Control Flow with Decisions and Loops 1 Flashcards
Explain what is happening in the following code sample.
name = “Alice”
if name = “Alice”:
print(“Hi Alice”)
- The string variable of name is set to the value of Alice
- If the if statement evaluates to True, then the following indented code is executed.
When is an else statement executed?
When the condition is False.
See the example below.
password = ‘Cisco’
if password =’Citrix’:
print(‘Access Granted’)
else:
print(‘Wrong password’)
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 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.
What will the following code evaluate to?
bool(0)
False
0 = False
1 = True
Describe what is happening with the following while loop.
spam = 0
while spam < 5:
print(‘Hello World’)
spam += 1
- The integer value of spam begins at zero
- 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.
- The while loop will ‘iterate’ five times.
Explain what will happen with the following while loop.
name = “ “
while name != “your name”:
print(“Please enter your name”)
name = input()
print(“Thank you”)
- The name variable is set to a blank string.
- The while condition will evaluate to True as the name variable does not equal “your name”. Therefore, the indented code will be evaluated next.
- The user will be prompted to enter their name, and the next iteration will occur.
- If the user did not enter the string “your name”, the while loop will continue to prompt for a name.
- 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”.
Explain what is happening with the following while loop.
while True:
print(“Hello”)
- The condition of the while loop will always evaluate to True and therefore, the program will print “Hello” each time.
- The loop will never evaluate to False, so it is an infinite loop.
What is the keyboard shortcut to end an infinite loop?
Ctrl+C
What purpose does a break statement serve when inserted into a while loop.
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.
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”)
- The while loop will always evaluate to True and prompt for user input.
- 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.
What function does a continue statement serve when used within a while loop?
- When a program reaches a continue statement within a while loop, the execution immediately jumps back to the start of the while loop.
- The program then starts to reevaluate the loops condition.
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))
- The spam integer variable starts at zero
- While spam is less than five, in other words True.
- The value of spam will be incremented by one.
- 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.
- Therefore, the output will be…
Spam is 1
Spam is 2
Spam is 4
Spam is 5
What is the fundamental difference between a while loop and a for loop?
- A while loop will iterate while a condition is True.
- A for loop will iterate a specific amount of times.
Explain what is happening with the following for loop.
print(“My name is”)
for i in range(5):
print(“Jimmy Five Times “ + str(i))
- The first print statement is executed.
- The value of i is set to zero.
- The indented print statement is executed and the value of i is concatenated to the string.
- The for loop will repeat 4 more times until the range condition is met.
- Remember, it will loop up to, and not including 5.
Using a for loop, write a script to add up every number from 1 to 100. Print the final total only.
total = 0
for num in range(101):
total = total + num
print(total)