Google Py While Loops
Can you work out what this function does? Try passing different parameters to the attempts function to see what it does.
Attempt is 1
Attempt is 2
Attempt is 3
Attempt is 4
Attempt is 5
Done
Question 1:
In this code, there's an initialization problem that's causing our
function to behave incorrectly. Can you find the problem and fix it?
Sol:
Here is your output:
3
2
1
Zero!
Question 1:
The following code causes an infinite loop.Here is your output:
1
2
3
4
5
Question 1
What are while loops in Python?
i. While loops let the computer execute a set of instructions while a condition is true.
ii. While loops instruct the computer to execute a piece of code a set number of times.
iii. While loops let us branch execution on whether or not a condition is true.
iv. While loops are how we initialize variables in Python.
Question 2
Fill in the blanks to make the print_prime_factors function print all the prime factors of a number. A prime factor is a number that is prime and divides another without a remainder.
Solution:
Question 3
The following code can lead to an infinite loop. Fix the code so that it can finish successfully for all numbers.
Note: Try running your function with the number 0 as the input, and see what you get!
Solution:
Questions 4
Fill in the empty function so that it returns the sum of all the divisors of a number, without including it. A divisor is a number that divides into another without a remainder.
Solution:
Question 5
The multiplication_table function prints the results of a number passed to it multiplied by 1 through 5.
An additional requirement is that the result is not to exceed 25, which is done with the break statement.
Fill in the blanks to complete the function to satisfy these conditions.
Solution:
3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 8x1=8 8x2=16 8x3=24
Queston: 6
Fill in the gaps of the sum_squares function, so that it returns the sum of all the squares of numbers between 0 and x (not included).
Remember that you can use the range(x) function to generate a sequence of numbers from 0 to x (not included).
Sulotion:
Here is your output:
285