Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 8 pages
Exam (elaborations)

Edhesive 4.1 - 4.11 Questions and Answers

Document preview thumbnail
Preview 2 out of 8 pages

Edhesive 4.1 - 4.11 Questions and Answers 4.1 Lesson Practice We use loops to: Repeat blocks of code 4.1 Lesson Practice 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? The line num = int(input("Enter a number, negative to stop")) should be indented so it is inside the loop 4.1 Lesson Practice Complete the activity. 4.1 Code Practice name = input("Please enter a name, Nope to end ") while (name != "Nope"): print ("Nice to meet you " + name) name = input("Please enter a name, Nope to end ") print ("Done") 4.2 Lesson Practice What is output? Select all that apply. c = 0 while (c 10): c = c + 5 print (c) 5 and 10 4.2 Lesson Practice What is output? Select all that apply. c = 3 while (c 10): c = c + 2 print (c) 5, 7, 9 and 11 4.2 Lesson Practice What is output? c = 1 sum = 0 while (c 10): c = c + 3 sum = sum + c print (sum) 21 4.2 Lesson Practice What should be entered to make the loop print 60 70 80 x = 50 while (x 80): x = x + ____ print (x) 10 4.2 Code Practice: Question 1 sum = 0 count = 0 while (sum = 100): count +=1 num = int(input("Enter a number: ")) sum += num print("Sum: " + str(sum)) print("Numbers entered: " + str(count)) 4.2 Code Practice: Question 2 pet = input("What pet do you have?") count = 0 while (pet!= "rock"): count +=1 print ("You have a " + str(pet) + " with a total of " + str(count) + " pet(s)") pet = input("Enter pet: ") 4.3 Lesson Practice Consider the following code: x = int(input ("Enter a value: ")) c = 0 sum = 0 while (c 10): x = int(input ("Enter a value: ")) c = c + 1 sum = sum + x print ("nnSum: " + str(sum)) What type of loop is it? Count variable loop 4.3 Lesson Practice This code loops until the user types in 17. It finds and prints the sum of all even numbers entered. n = int(input ("Enter a number, 17 to stop.")) sum = 0 while (n != 17): if (n % 2 == 0): sum = sum + n n = int(input ("Enter a number, 17 to stop.")) print ("Sum: " + str(sum)) This is an example of a(n) ____ loop user input 4.3 Lesson Practice Suppose you change the program in the last problem so the user enters exactly 17 numbers. The loop would then be an example of a(n) _____ loop count variable 4.3 Code Practice: Question 1 age = int(input("How old are you?")) for i in range(1,age+1): print("*HUG*") 4.3 Code Practice: Question 2 i = 3 while i =21: if i % 3 == 0: print(i) i+=1 4.5 Lesson Practice We use loops: To repeat a section of code 4.5 Lesson Practice What are the two ways to end a loop? (Select two answers.) Count variable Using user input 4.5 Code Practice (dont reccomened using this) i = 0 while True: word=input("Please enter the next word: ") if word == "STOP": break i += 1 print("#{}:You entered{}".format(i, word)) print("All done. {} words entered.".format(i)) 4.6 Lesson Practice Range is an example of a ______________. function 4.6 Lesson Practice The range function must have two parameters? False 4.6 Lesson Practice What list of numbers is created by the following code? range(8) 0 1 2 3 4 5 6 7 4.6 Lesson Practice What list of numbers is created by the following code? range(4, 11) 4 5 6 7 8 9 10 4.6 Lesson Practice n Practice Which range function creates the following list of numbers? 39 range(76, 60, -2) 4.7 Lesson Practice A for loop is used to replace a ________ while loop. counting 4.7 Lesson Practice The following loop is intended to print the numbers 1, 2, 3, 4, 5.Fill in the blank: for i in _____ (1, 6): print (i) range 4.7 Lesson Practice When do you use a for loop instead of a while loop? (Select multiple answers) You know how many times you want the loop to run When there is a definite starting and ending point 4.7 Lesson Practice What is wrong with the following code? for range(7, 10): print(x) The first line should be for x in range(7, 10): 4.7 Lesson Practice What is output by the following code? for x in range(7, 16): print(x * 2, end=" ") 4.7 Code Practice: Question 1 for i in range(20, 31): print(i, end=" ") 4.7 Code Practice: Question 2 for i in range(56, 71): print(i, end=" ") 4.8 Lesson Practice Which range function will create a list with all odd numbers between 5 and 77? range(5, 78, 2) 4.8 Lesson Practice What is output by the following code: for i in range(41, 31, -1): print(i) What is output? All numbers from 41 to 32 counting counting backwards. 4.8 Lesson Practice Consider the following code: for r in range (10, 60, 10): print("Hello") How many times is the word "Hello" output? 5 4.8 Code Practice: Question 1 for I in range(5, 76, 5): print(I, end=" ") 4.8 Code Practice: Question 2 for i in range(88,40,-4): print(i, end=' ') 4.8 Code Practice: Question 3 for i in range(200, 300+1, 2): print(i) 4.9 Lesson Practice ____________ means to set a variable equal to a known value before a loop. Initialize 4.9 Lesson Practice Consider the following code: start = int(input("Enter the starting number: ")) stop = int(input("Enter the ending number: ")) x = -10 sum = 0 for i in range (start, stop, x): sum = sum + i print(sum) What is output if the user enters 78 then 45? 252 4.9 Lesson Practice What is output by the following code? sum = 0 for i in range (3, 15, 3): sum = sum + i print ("Sum = " +str( sum)) Sum = 30 4.9 Lesson Practice A ___________ variable is used to add up a set of values. sum 4.9 Code Practice: Question 1 out = 0 for i in range(3, 67, 3): out += i print(out) 4.9 Code Practice: Question 2 count = 0 for x in range(20, 100, 10): count += x print(count) 4.9 Code Practice: Question 3 result = 0 for i in range(99, 0, -1): result += i print(result) 4.9 Code Practice: Question 4 print('Enter ten temperatures please') sum = 0 for i in range(10): T = float(input()) sum = sum + T print('Sum is: ',sum)

Content preview

Edhesive 4.1 - 4.11 Questions and
Answers
4.1 Lesson Practice

We use loops to: - answer Repeat blocks of code

4.1 Lesson Practice

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? - answer The line num = int(input("Enter a number, negative to
stop")) should be indented so it is inside the loop

4.1 Lesson Practice - answer Complete the activity.

4.1 Code Practice - answer name = input("Please enter a name, Nope to end ")
while (name != "Nope"):
print ("Nice to meet you " + name)
name = input("Please enter a name, Nope to end ")
print ("Done")

4.2 Lesson Practice

What is output? Select all that apply.
c=0
while (c < 10):
c=c+5
print (c) - answer 5 and 10

4.2 Lesson Practice

What is output? Select all that apply.
c=3
while (c < 10):
c=c+2
print (c) - answer 5, 7, 9 and 11

4.2 Lesson Practice

, What is output?
c=1
sum = 0
while (c < 10):
c=c+3
sum = sum + c
print (sum) - answer 21

4.2 Lesson Practice

What should be entered to make the loop print
60
70
80
x = 50
while (x < 80):
x = x + ____
print (x) - answer 10

4.2 Code Practice: Question 1 - answer sum = 0
count = 0
while (sum <= 100):
count +=1
num = int(input("Enter a number: "))
sum += num
print("Sum: " + str(sum))
print("Numbers entered: " + str(count))

4.2 Code Practice: Question 2 - answer pet = input("What pet do you have?")
count = 0
while (pet!= "rock"):
count +=1
print ("You have a " + str(pet) + " with a total of " + str(count) + " pet(s)")
pet = input("Enter pet: ")

4.3 Lesson Practice

Consider the following code:
x = int(input ("Enter a value: "))
c=0
sum = 0
while (c < 10):
x = int(input ("Enter a value: "))
c=c+1
sum = sum + x

Document information

Uploaded on
January 3, 2025
Number of pages
8
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$13.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Pogba119
3.9
(14)
Sold
62
Followers
2
Items
5513
Last sold
5 days ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions