Question: Python Exercises Repetition PAGE 1 1 . Mark the following statements as true or false: a . In a counter - controlled while loop, it
Python Exercises
Repetition
PAGE
Mark the following statements as true or false:
a In a countercontrolled while loop, it is not necessary to initialize the loop control variable.
b It is possible that the body of a while loop might not execute at all.
c In an infinite while loop, the loop condition is initially false, but after the first iteration, it is always
true.
d The while loop:
j
while j :
j j
terminates when j
e A sentinelcontrolled while loop is an eventcontrolled while loop whose termination depends on a
special value.
f A loop is a control structure that causes certain statements to execute over and over.
g To read data from a file of an unspecified length, an EOFcontrolled loop is a good choice.
h When a while loop terminates, the control first goes back to the statement just before the while
statement, and then the control goes to the statement immediately following the while loop.
What is the output of the following Python code?
i
temp
while i :
temp temp i
i i
What is the output of the following Python code?
num
while num :
num num
printnum
What is the output of the following Python code?
num
while num num num:
num num
printnum
When does the following while loop terminate?
num
while num and num :
num num
num floatnum
printnum
Suppose that the input is What is the output of the following code?
sum intinputEnter sum:
count intinputEnter count:
while count :
num intinputEnter num:
sum sum num
count
printSum strsum
Python Exercises
Repetition
PAGE
Suppose that the input is What is the output of the following code?
sum intinputEnter sum:
num sum
while num :
num intinputEnter num:
sum sum num
printSum strsum
Suppose that the input is What is the output of the following code?
num intinputEnter num:
sum num
while num :
sum sum num
num intinputEnter num:
printSum str sum
Suppose that the input is: What is the output of the following code? Assume all variables
are properly declared.
sum
number intinputEnter numbers to sum enter to quit:
while number :
sum sum number
num intinputEnter next number:
printsum sum
Correct the following code so that it reads and finds the sum of numbers:
count
sum
while count :
num intinputEnter next number:
count
sum count
What is the output of the following program?
x
y
z y
while z x:
printz
z z
Suppose that the input is : What is the output of the
following program?
count
num intinputEnter num:
while num :
count
printnum count
num intinputEnter num:
print
return
Python Exercises
Repetition
PAGE
The following program is designed to input two numbers and output their sum. It asks the user if heshe
would like to run the program. If the answer is Y or y it prompts the user to enter two numbers. After adding
the numbers and displaying the results, it again asks the user if heshe would like to add more numbers.
However, the program fails to do so Correct the program so that it works properly.
printThis program adds two numbers."
response inputWould you like to run the program: Yy
while response Y or response y:
num intinputEnter the first number:
num intinputEnter the second number:
printf numf numf num num
response inputWould you like to add again: Yy
break
What is the output of the following program segment?
count
while count :
printcount: strcount
What is the output of the following program segment?
count
while count :
count count
printcount
What is the output of the following program segment?
count
while count :
printcount: strcount
What is the output of the following program segment?
count
while count :
count count
printcount count
What type of loop, such as countercontrol or sentinelcontrol, will you use in each of the following
situations?
a Sum the following series:
b Sum the following numbers, except the last number:
c A file contains an employees salary. Update the employees salary.
Consider the following for loop:
s
for j in range:
s s j j
In this for loop, identify the loop control variable, the initialization statement, lo
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
