Trace the loop, writing updated values for every iteration or until you identify the pattern
Subsection6.6.1Evaluate Loops-WE3-P1
ExercisesExercises
1.
Q11: Put the code in the right order to create a program that will continue to generate integers between 1 and 100 (inclusive) until the value generated is less than 6 or greater than 95. Print the numbers generated.
from random import randint
---
repeat = True
---
while repeat:
---
x = randint(1, 100)
---
print("Value is", x)
---
repeat = x >= 6 and x <= 95
2.
Q12: What is the output of the following code if the user enters -10, 100, 101, 54, 1, 98, 0?
num_bad_inputs = 0
num = int(input("Input a number between 1 and 100: "))
while num < 1 or num > 100:
num_bad_inputs += 1
num = int(input("Input a number between 1 and 100: "))
print("failed inputs: " + num_bad_inputs)
failed inputs: 2
failed inputs: 3
failed inputs: 4
failed inputs: 5
no output
3.
Q13: What is the output of the following code if the user enters 12 18 49 36 50 -1?
evens = 0
odds = 0
num = int(input("Enter a number: "))
while num > 0 and num < 50:
if num % 2 == 0:
evens += 1
else:
odds += 1
num = int(input("Enter a number: "))
print(evens + "even(s) entered and " + odds + " odd(s) entered")
3 even(s) entered and 2 odd(s) entered
3 even(s) entered and 1 odd(s) entered
4 even(s) entered and 2 odd(s) entered
4 even(s) entered and 1 odd(s) entered
Compiler Error
4.
Q14: What is the output of the following code if the user enters 98 68 82 77 45 100 104?
sum = 0
num_scores = 0
num = int(input(“Score: ”))
while num >= 0 and num <= 100:
sum += num
num_scores += 1
num = int(input(“Score: ”))
print(sum / num_scores)
74
92.5
81
81.25
78.33
5.
Q15: What is the output of the following code?
a = 10
b = 5
counter = 1
while counter < a or counter < b:
if counter % 2 == 0:
counter *= 2
else:
counter += 1
print(counter)