3-1-3-23. Quiz: While Loops

Quiz: Count By

Suppose you want to count from some number start_num by another number count_by until you hit a final number end_num. Use break_num as the variable that you’ll change each time through the loop. For simplicity, assume that end_num is always larger than start_num and count_by is always positive.

Before the loop, what do you want to set break_num equal to? How do you want to change break_num each time through the loop? What condition will you use to see when it’s time to stop looping?

After the loop is done, print out break_num, showing the value that indicated it was time to stop looping. It is the case that break_num should be a number that is the first number larger than end_num.

start_num = #provide some start number
end_num = #provide some end number that you stop when you hit
count_by = #provide some number to count by 

# write a while loop that uses break_num as the ongoing number to 
#   check against end_num


print(break_num)

Quiz: Count By Check

Suppose you want to count from some number start_num by another number count_by until you hit a final number end_num, and calculate break_num the way you did in the last quiz.

Now in addition, address what would happen if someone gives a start_num that is greater than end_num. If this is the case, set result to "Oops! Looks like your start value is greater than the end value. Please try again." Otherwise, set result to the value of break_num.

start_num = #provide some start number
end_num = #provide some end number that you stop when you hit
count_by = #provide some number to count by 

# write a condition to check that end_num is larger than start_num before looping
# write a while loop that uses break_num as the ongoing number to 
#   check against end_num


print(result)

Quiz: Nearest Square

Write a while loop that finds the largest square number less than an integerlimit and stores it in a variable nearest_square. A square number is the product of an integer multiplied by itself, for example 36 is a square number because it equals 6*6.

For example, if limit is 40, your code should set the nearest_square to 36.

limit = 40

# write your while loop here


print(nearest_square)

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading