Unit 4: Repetition and Loops

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

4.3 Code Practice: Question 1 Grandma Ester normally bakes you cookies for your birthday - one for every year old you are. When you turned 15, she made you 15 cookies! This year, she, unfortunately, cannot see you on your birthday, so instead, she wants to send you virtual cookies! Create a program that prompts the user for how old they are turning, and then, using a loop, outputs a virtual cookie for every year old they are. The following prints a single "virtual cookie."

age = int(input("How old are you? ")) for i in range(age): print("(::)")

4.1 Code Practice Write a program that asks the user to enter a city name, and then prints Oh! CITY is a cool spot. Your program should repeat these steps until the user inputs Nope.

city1 = input ("Please enter a city name: (Nope to end) ") while city1 != "Nope" : print("Oh! " + city1 + " is a cool spot.") city1 = input("Please enter a city name: (Nope to end) ") print("Nope")

4.8 Code Practice: Question 1 Write a for loop to print the numbers 0, 7, 14, 21 ... 77 on one line.

for i in range(0,84,7): print(i, end = " ")

4.7 Code Practice: Question 2 Write a loop to print 26 to 40 inclusive (this means it should include both the 26 and 40). The output should all be written out on the same line.

for i in range(26, 41): print(i, end = " ")

4.7 Code Practice: Question 1 Write a for loop to print the numbers from 35 to 45, inclusive (this means it should include both the 35 and 45). The output should all be written out on the same line.

for i in range(35, 46): print(i, end = " ")

4.8 Code Practice: Question 3 Write a for loop to print all the even numbers from 350 to 450.

for i in range(350,450+1,2): print(i)

4.8 Code Practice: Question 2 Write a for loop to print the numbers 76, 70, 64, ...34 on one line.

for i in range(76,28,-6): print(i, end = " ")

4.2 Code Practice: Question 1

.

4.2 Code Practice: Question 2 Write a loop that continually asks the user what pets the user has until the user enters stop, in which case the loop ends. It should acknowledge the user in the following format. For the first pet, it should say You have one cat. Total # of Pets: 1 if they enter cat, and so on.

.

4.5 Code Practice Write a loop that inputs words until the user enters DONE. After each input, the program should number each entry and print in this format: #1: You entered _____ When DONE is entered, the total number of words entered should be printed in this format: A total of __ words were entered.

.

4.2 Lesson Practice Question 1: What is output by the following code? Select all that apply. c = 0 while (c < 11): c = c + 6 print (c) Question 2: What is output by the following code? Select all that apply. c = 2 while (c < 12): print (c) c = c + 3 Question 3: What is output by the following code? c = 1 sum = 0 while (c < 10): c = c + 2 sum = sum + c print (sum) Question 4: What should be entered to make the loop below print 55 60 65 x = 50 while (x < 65): x = x + ____ print (x)

1: 6 & 12 2: 2, 5, 8, & 11 3: 35 4: 5

4.1 Lesson Practice Question 1: Loops can be used to __________ Question 2: Consider the following code: num = int(input("Enter a number, negative to stop")) while (num <= 0): print ("You entered: " + str(num)) num = int(input("Enter a number, negative to stop")) print ("Done.") What is wrong with the code?

1: Repeat sections of code 2: The test condition num <= 0 is not correct

4.3 Lesson Practice Question 1: Consider the following code: x = int(input ("Enter a value: ")) c = 0 sum = 0 while (x != 0): x = int(input ("Enter a value: ")) c = c + 1 sum = sum + x print ("\n\nSum: " + str(sum)) What of the following best describe the loop? Question 2: This code loops until c reaches a value of 10. It finds and prints the sum of all odd numbers entered. n = int(input ("Enter a number, -1 to stop.")) c = 1 sum = 0 while (c != 10): if (n % 2 == 1): sum = sum + n n = int(input ("Enter a number, -1 to stop.")) c = c + 1 print ("Sum: " + str(sum)) This is an example of a(n) __________ loop. Question 3: Suppose you change the program in the last problem, so the user enters numbers until 7 is entered. The loop would then be an example of a(n) _____ loop

1: User input loop 2: count variable 3: user input

4.7 Lesson Practice Question 1: A _________ while loop can be replaced by a for loop. Question 2: The following loop is intended to print the numbers 2 4 6 8 10 Fill in the blank: for i in ________: print (i) Question 3: When do you use while loop instead of a for loop? Question 4: What is wrong with the following code? for i in (7, 10): print(i) Question 5: What is output by the following code? for x in range(6, 16, 2): print(x // 2, end=" ")

1: counting 2: range(2,11,2) 3: To make sure a correct number is entered & To loop until a certain condition is reached 4: The first line should be for i in range(7, 10): 5: 3 4 5 6 7

4.6 Lesson Practice Question 1: An example of a function is ___________ Question 2: The range function has to be called with three parameters Question 3: What list of numbers is created by the following code: range(9) Question 4: What list of numbers is created by the following code: range(7,16) Question 5: Which range function creates the following list of numbers? 21 25 29 33 37 41 Question 6: Which range function creates the following list of numbers? 91 86 81 76 71 66 61

1: range 2: False 3: 0 1 2 3 4 5 6 7 8 4: 7 8 9 10 11 12 13 14 15 5: range(21, 44, 4) 6: range(91, 60, -5)

4.8 Lesson Practice Question 1: Which range function will create a list with all even numbers between 8 and 42, inclusive? Question 2: What is output by the following code? for i in range(73, 43, -2): print(i) Question 3: Consider the following code: for r in range (15, 45, 15): print("Goodbye") How many times is the word "Goodbye" output?

1: range(8, 44, 2) 2: All the numbers from 73 to 45 counting backwards by 2 3: 2

4.5 Lesson Practice Question 1: Loops are used to ___________ Question 2: Which of the following are valid ways to end a loop? Select all that apply

1: repeat code segments 2: Checking when a variable reaches a certain value & Using user input

4.3 Code Practice: Question 2 Write a program that uses a while loop to calculate and print the multiples of 4 from 4 to 24. Your program should print each number on a separate line.

n = 4 while n <= 24: print(n) n+=4


संबंधित स्टडी सेट्स

Knowledge Check: BOH Practice Test 3Q22.0

View Set

Chapter 34: The School-Age Child and Family

View Set

Business Law Chapter 39: Consumer and Environmental Law

View Set

Marketing 301 Final (Ch 16, 17, 18 & 19)

View Set

Chapter 11.6 Cyclical Unemployment

View Set