Google Py While Loops

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.  

 

def attempts(n):
x = 1
while x <= n:
print("Attempt is " + str(x))
x += 1
print("Done")
attempts(5)
 
 
Output: 
 
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?
 
def count_down(start_number):
while (current > 0):
print(current)
current -= 1
print("Zero!")

count_down(3)
 
 
Sol:
def count_down(start_number):
while (start_number > 0):
print(start_number)
start_number -= 1
print("Zero!")

count_down(3)
 
 
Here is your output:
3
2
1
Zero!

 
 
Question 1:
The following code causes an infinite loop.
Can you figure out what’s missing and how to fix it?


def print_range(start, end):
# Loop through the numbers from start to end
n = start
while n <= end:
print(n)

print_range(1, 5) # Should print 1 2 3 4 5 (each number on its own line)




Solution:
 
def print_range(start, end):
# Loop through the numbers from start to end
n = start
while n <= end:
print(n)
n += 1

print_range(1, 5) # Should print 1 2 3 4 5 (each number on its own line) 
 
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:

def print_prime_factors(number):
# Start with two, which is the first prime
factor = 2
# Keep going until the factor is larger than the number
while factor <= number:
# Check if factor is a divisor of number
if number % factor == 0:
# If it is, print it and divide the original number
print(factor)
number = number / factor
else:
# If it's not, increment the factor by one
factor += 1
return "Done"

print_prime_factors(100)
# Should print 2,2,5,5
# DO NOT DELETE THIS COMMENT
 


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:

 

def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
if n == 0:
return False
else:
while n % 2 == 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
return False

print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False

 

 

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:


def sum_divisors(n):
i = 1
sum = 0
# Return the sum of all divisors of n, not including n
while i < n:
if n % i == 0:
sum = sum + i
i = i + 1
else:
i = i + 1
return sum

print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114


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.

 

def multiplication_table(number):
# Initialize the starting point of the multiplication table
multiplier = 1
# Only want to loop through 5
while multiplier <= 5:
result = ___
# What is the additional condition to exit out of the loop?
if ___ :
break
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the variable for the loop
___ += 1

multiplication_table(3)
# Should print: 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15

multiplication_table(5)
# Should print: 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25

multiplication_table(8)
# Should print: 8x1=8 8x2=16 8x3=24


Solution:

def multiplication_table(number):
# Initialize the starting point of the multiplication table
multiplier = 1
# Only want to loop through 5
while multiplier <= 5:
result = number * multiplier
# What is the additional condition to exit out of the loop?
if result > 25 :
break
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the variable for the loop
multiplier += 1

multiplication_table(3)
# Should print: 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15

multiplication_table(5)
# Should print: 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25

multiplication_table(8)
# Should print: 8x1=8 8x2=16 8x3=24
 
Output: 
 
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). 

def square(n):
return n*n

def sum_squares(x):
sum = 0
for n in ___:
sum += __
return __

print(sum_squares(10)) # Should be 285


Sulotion:


def square(n):
return n*n

def sum_squares(x):
sum = 0
for n in range(x):
sum += int(square(n))
return sum

print(sum_squares(10)) # Should be 285


Here is your output:
285


إرسال تعليق (0)
أحدث أقدم