Python CHAPTER 3
What will be displayed after the following code is executed? counter = 1 while counter <= 20: print(counter, end=" ") counter *= 3 print("\nThe loop has ended.")
1 3 9 The loop has ended.
Which of the following is true for an if statement that has both elif and else clauses?
If the condition in the if clause is true, the statements in that clause are executed.
What will the output of the following code be if the user enters 3 at the prompt? your_num = int(input("Enter a number:")) while (your_num > 0): product = your_num * 5 print(your_num, " * 5 = ", product) your_num -= 1
3 * 5 = 15 2 * 5 = 10 1 * 5 = 5
How many times will "Hi there!" be displayed after the following code executes? num = 2 while num < 12: print("Hi, there!") num += 2
5
What will this loop display? sum = 0 for i in range(0, 25, 5): sum += i print(sum)
50
How many times will "Hi again!" be displayed after the following code executes? for i in range(0, 12, 2): print("Hi, again!")
6
In a while loop, the Boolean expression is tested
before the loop is executed
To jump to the end of the current loop, you can use the
break statement
Given the following code, select the compound condition that makes sure that the input is an integer greater than 0 and less than 1,000. my_num = input("Enter a number between 1 and 999:") check_num = int(my_num) while _________________________: my_num = input("Try again. The number must be between 1 and 999") check_num = int(my_num)
check_num <= 0 or check_num >= 1000
A for loop that uses a range() function is executed
once for each integer in the collection returned by the range() function
Pseudocode can be used to plan
the coding of control structures
For the following code, what will the result be if the user enters 5 at the prompt? sum = 0 end_value = int(input("Enter a number: ")) for i in range(1, end_value): sum = sum + i print(sum, end=", ")
1, 3, 6, 10,
Python will sort the strings that follow in this sequence: Peach peach 1peach 10Peaches
10Peaches, 1peach, Peach, peach
Which type of expression has a value of either true or false?
Boolean
Code Example 3-1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. Which of the following could be a pseudocode plan for the program?
Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results
For the following code, what will the result be if the user enters 4 at the prompt? product = 1 end_value = int(input("Enter a number: ")) for i in range(1, end_value+1): product = product * i i += 1 print("The product is ", product)
The product is 24
For the following code, what will the result be if the user enters 4 at the prompt? product = 1 end_value = int(input("Enter a number: ")) for i in range(1, end_value): product = product * i i += 1 print("The product is ", product)
The product is 6
Code Example 3-1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "Y" at the prompt, what does the program print to the console?
You bought 0 widget(s) today.
Code Example 3-1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "no" at the prompt, what does the program print to the console?
You bought 0 widget(s) today.
Which of the following can use the range() function to loop through a block of statements?
a for statement
If you want to code an if clause, but you don't want to perform any action, you can code
a pass statement
Which of the following begins by testing a condition defined by a Boolean expression and then executes a block of statements if the condition is true?
a while statement
Which of the following in this expression is evaluated first? age >= 65 and status == "retired" or age < 18
age >= 65
Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired"
age >= 65
The and operator has
higher precedence than the or operator, but lower precedence than the not operator
Which of the following creates a Boolean variable?
if flag == True:
To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the
lower() method to convert all characters in each string to lowercase.
Which of the following is not an acceptable way to code a nested structure?
nest an else clause within an elif clause
What will be displayed after the following code executes? guess = 19 if guess < 19: print("Too low") elif guess > 19: print("Too high")
nothing will dsplay
When two strings are compared, they are evaluated based on the ___________________ of the characters in the string.
sort sequence