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 1
1. Mark the following statements as true or false:
a. In a counter-controlled 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 =0
while (j <=10):
j = j +1
terminates when j >10
e. A sentinel-controlled while loop is an event-controlled 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 EOF-controlled 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.
2. What is the output of the following Python code?
i =2
temp =1
while i <10:
temp = temp *(i 1)
i = i +1
3. What is the output of the following Python code?
num =5
while (num >5):
num = num +2
print(num)
4. What is the output of the following Python code?
num =0
while num * num <=6* num:
num = num +1
print(num )
5. When does the following while loop terminate?
num =4
while 1<= num and num <=20:
num =(num)+1
num = float(num)
print(num)
6. Suppose that the input is 1203515286-1. What is the output of the following code?
sum = int(input(Enter sum: ))
count = int(input(Enter count: ))
while count <=4:
num = int(input(Enter num: ))
sum = sum + num
count +=1
print(Sum =+ str(sum))
Python Exercises
Repetition
PAGE 2
7. Suppose that the input is 34672-1. What is the output of the following code?
sum = int(input(Enter sum: ))
num = sum
while num !=-1:
num = int(input(Enter num: )
sum = sum +2* num
print(Sum =+ str(sum))
8. Suppose that the input is 34672-1. What is the output of the following code?
num = int(input(Enter num: ))
sum = num
while num !=-1:
sum = sum +2* num
num = int(input(Enter num: ))
print(Sum =+ str( sum))
9. Suppose that the input is: 38357114-1.What is the output of the following code? Assume all variables
are properly declared.
sum =0
number = int(input("Enter numbers to sum (enter -1 to quit): "))
while number !=-1:
sum = sum + number
num= int(input('Enter next number: '))
print("sum =",sum)
10. Correct the following code so that it reads and finds the sum of 20 numbers:
count =0
sum =0
while count <=20:
num= int(input('Enter next number: '))
count +=1
sum += count
11. What is the output of the following program?
x =4
y =5
z = y +6
while (((z - x)%4)!=0):
print(z ,"")
z = z +7
12. Suppose that the input is :582314759815012176145-999. What is the output of the
following program?
count =0
num = int(input(Enter num: ))
while num !=-999:
count +=1
print(num % count +)
num = int(input(Enter num: ))
print(
)
return 0
Python Exercises
Repetition
PAGE 3
13. The following program is designed to input two numbers and output their sum. It asks the user if he/she
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 he/she would like to add more numbers.
However, the program fails to do so. Correct the program so that it works properly.
print("This program adds two numbers.")
response = input("Would you like to run the program: (Y/y)")
while (response =='Y' or response =='y'):
num1= int(input("Enter the first number: "))
num2= int(input("Enter the second number: "))
print("%.2f "% num1,"%.2f "% num2,"%.2f "%(num1- num2))
response = input("Would you like to add again: (Y/y)")
break
14. What is the output of the following program segment?
count =1
while count +=1<=5:
print(count: + str(count))
15. What is the output of the following program segment?
count =5
while (count >0):
count = count-1
print(count,"")
16. What is the output of the following program segment?
count =5
while count -=1>0:
print(count: + str(count))
17. What is the output of the following program segment?
count =1
while (count <=5):
count = count +1
print((count *(count -2)),"")
18. What type of loop, such as counter-control or sentinel-control, will you use in each of the following
situations?
a. Sum the following series: 1+(2/1)+(3/2)+(4/3)+(5/4)+...+(10/9)
b. Sum the following numbers, except the last number: 17,32,62,48,58,-1
c. A file contains an employees salary. Update the employees salary.
19. Consider the following for loop:
s =0
for j in range(0,10):
s = s + j *(j -1)
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!