Edhesive 4.1 - 4.11
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.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.7 Lesson Practice What is output by the following code? for x in range(7, 16): print(x * 2, end=" ")
14 16 18 20 22 24 26 28 30
4.2 Lesson Practice What is output? c = 1 sum = 0 while (c < 10): c = c + 3 sum = sum + c print (sum)
21
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.10 Lesson Practice Questions 1 - 3 refer to the following code: maximum = 0 for i in range (10): n = int(input("Enter a number: ")) if (n >= 0 and n > maximum): maximum = n print(maximum) How many variables are in the program?
3
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.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.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.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.11 Lesson Practice Computer simulations were first developed during as a part of the .
Answer 1:WWII Answer 2:Manhattan Project
4.1 Lesson Practice
Complete the activity.
4.5 Lesson Practice What are the two ways to end a loop? (Select two answers.)
Count variable Using user input
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 ("\n\nSum: " + str(sum)) What type of loop is it?
Count variable loop
4.6 Lesson Practice The range function must have two parameters?
False
4.10 Lesson Practice Trace the code. What does the program do?
Finds the largest positive number of the numbers entered
4.9 Lesson Practice ____________ means to set a variable equal to a known value before a loop.
Initialize
4.11 Lesson Practice _________ _________ _________ use random numbers made by computers to test computer models.
Monte Carlo Methods
4.1 Lesson Practice We use loops to:
Repeat blocks of code
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.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.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.11 Lesson Practice Which of the following is NOT a use of computer simulations:
To calculate the area of an oval room
4.5 Lesson Practice We use loops:
To repeat a section of code
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.10 Lesson Practice What would happen if only negative numbers are entered in the program?
Zero is output.
4.3 Code Practice: Question 1
age = int(input("How old are you?")) for i in range(1,age+1): print("**HUG**")
4.9 Code Practice: Question 2
count = 0 for x in range(20, 100, 10): count += x print(count)
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.7 Lesson Practice A for loop is used to replace a ________ while loop.
counting
4.8 Code Practice: Question 1
for I in range(5, 76, 5): print(I, end=" ")
4.7 Code Practice: Question 1
for i in range(20, 31): print(i, end=" ")
4.8 Code Practice: Question 3
for i in range(200, 300+1, 2): print(i)
4.7 Code Practice: Question 2
for i in range(56, 71): print(i, end=" ")
4.8 Code Practice: Question 2
for i in range(88,40,-4): print(i, end=' ')
4.6 Lesson Practice Range is an example of a ______________.
function
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.3 Code Practice: Question 2
i = 3 while i <=21: if i % 3 == 0: print(i) i+=1
4.11 Lesson Practice Computer __________ are used to represent a real world situation using a computer.
models
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.9 Code Practice: Question 1
out = 0 for i in range(3, 67, 3): out += i print(out)
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.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)
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.8 Lesson Practice Which range function will create a list with all odd numbers between 5 and 77?
range(5, 78, 2)
4.6 Lesson Practice n Practice Which range function creates the following list of numbers? 24 27 30 33 36 39
range(76, 60, -2)
4.9 Code Practice: Question 3
result = 0 for i in range(99, 0, -1): result += i print(result)
4.11 Lesson Practice ____________ run experiments using computer models.
simulations
4.9 Lesson Practice A ___________ variable is used to add up a set of values.
sum
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.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