Unit 4: Repetition and Loops

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

A ___________ variable is used to add up a set of values.

sum

What is output by the following code: for x in range (8, 16): print (x * 2, end=" ")

16 18 20 22 24 26 28 30

Consider the following code: start = int(input("Enter the starting number: ")) stop = int(input("Enter the ending number: ")) x = -5 sum = 0 for i in range (start, stop, x): sum = sum + i print (sum) What is output if the user enters 18 then 13?1

18

Consider the following code: start = int(input("Enter the starting number: "))stop = int(input("Enter the ending number: "))x = -10sum = 0for i in range (start, stop, x):sum = sum + iprint(sum) What is output if the user enters 78 then 45?

252

Questions 1 - 3 refer to the following code: maximum = 0for i in range (10):n = int(input("Enter a number: "))if (n >= 0 and n > maximum):maximum = nprint(maximum) How many variables are in the program?

3

What is output? Select all that apply. c = 3 while (c < 10): c = c + 2 print (c)

5 7 9 11

What does the following loop do? val = 0 total = 0 while (val < 10): val = val + 1 total = total + val print(total)

Prints the sum of the numbers from 1 to 10.

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 y in range(88, 43, -4): print(y, end=" ")

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)

Which of the following is NOT true about while loops?

Used to find remainders

What are the two ways to end a loop? (Select two answers.)

Using user input Count variable

Which loop prints the numbers 1, 2...100?

c = 1 while (c <= 100): print (c) c = c + 1

4.3 Code Practice: Question 2

c = 3 while (c < 23): print(c) c = c + 3

Information sent to a function is a?

parameter

What should be entered to make the loop print 607080 x = 50 while (x < 80): x = x + ____ print (x)

10

Consider the following code: start = int(input("Enter the starting number: ")) stop = int(input("Enter the ending number: ")) x = 6 for i in range (start, stop, x): print (i, end=" ") What is output if the user enters 12 then 36?

12 18 24 30

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

What is output? Select all that apply. c = 6 while (c > 0): print (c) c = c - 2

2 4 6

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

21

Consider the following code: for r in range (10, 60, 10):print("Hello") How many times is the word "Hello" output?

5

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

5 10

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.

The range function must have two parameters?

False

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))

Count variable loop

Trace the code. What does the program do?

Finds the largest positive number of the numbers entered

We use loops to:

Repeat blocks of code

Consider the following code: for range (15, 88): print(x) What is wrong with this program?

The first line should be for x in range(15, 88):

What is wrong with the following code? for range(7, 10):print(x)

The first line should be for x in range(7, 10):

Consider the following code: for x (7, 10): print (x) What is wrong with the program?

The first line should be for x in range(7, 10):.

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

Which of the following is NOT a rule for algorithms?

They repeat indefinitely.

Computer simulations were first developed during ___ as a part of the ___.

WWII Manhattan Project

When do you use a while loop instead of a for loop? (Select multiple answers)

You do not know how many times a loop will need to run. When using a count variable

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

When do you use a for loop instead of a while loop? (There may be more than one answer.)

You know how many times you want the loop to run. When there is a defined start and end.

What would happen if only negative numbers are entered in the program?

Zero is output.

For a loop to stop, the loop control variable must ______________.

change inside the loop

4.9 Code Practice: Question 2

count = 0 for x in range(20, 100, 10): count += x print(count)

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

A ______ loop has a built in count variable.

for

4.8 Code Practice: Question 1

for I in range(5, 76, 5): print(I, end=" ")

A ____________ is a collection of commands given a name.

function

Range is an example of a ______________.

function

Setting a starting value for a variable is called _____________.

initializing

Which range function creates the following list of numbers? 76 74 72 70 68 66 64 62

range(76, 60, -2)

Computer __________ are used to represent a real world situation using a computer.

models

4.5 Code Practice

n= input("Please enter the next word: ") x=1 while(n != "STOP"): print("#" + str(x) + ": You entered " + n) x=x+1 n= input("Please enter the next word: ") print("All done. " + str(x-1) + " words entered.")

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? ") c=1 while(pet!="rock"): print("You have a " + pet + " with a total of " + str(c) + " pet(s)") pet = input("What pet do you have?") c = c + 1

4.9 Code Practice: Question 3

result = 0 for i in range(99, 0, -1): result += i print(result)

____________ run experiments using computer models.

simulations

4.2 Code Practice: Question 1

u=float(input("Enter a number")) c = 1 while (u <= 100): n = float(input("Enter a number")) u = u + n c = c + 1 print ("Sum: " + str (u)) if(u > 100): print ("Numbers Entered: " + str(c))

In a(n) __________________ loop, you do not know ahead of time how many times the loop will repeat.

user input

Consider the following code: x = 9 sum = 0 while (x < 19): x = x + 1 num = int(input("Enter a number: ")) sum = sum + num print(str(x) + ": " + str(num) + " Total: " + str(sum)) Which of the following is the loop control variable?

x

Consider the following code: for i in range (x, y): print (i, end=" ") What values for variables x and y will produce the output below? 5 6 7 8 9 10 11 12 13 14

x = 5 y = 15

What list of numbers is created by the following code? range(8)

0 1 2 3 4 5 6 7

What is output by the following code? for x in range (10): print(x, end=" ")

0 1 2 3 4 5 6 7 8 9

What is output by the following code? for x in range (25, 5, -2): print(x, end=" ")

25 23 21 19 17 15 13 11 9 7

The two ways to end a loop are:

Count variables User input

____________ means to set a variable equal to a known value before a loop.

Initialize

_________ _________ _________ use random numbers made by computers to test computer models.

Monte Carlo Methods

We use loops:

To repeat a section of code

What is output by the following code? for x in range (4): for y in range (3): print("*", end=" ") print("")

* * * * * * * * * * * *

4.1 Code Practice

name = input("Enter a name, type Nope to end loop: ") while (name != "Nope"): print("Nice to meet you " + name) name = input("Enter a name, type Nope to end loop: ") print("Done")

Write a program that asks the user for their name and how many times to print it. If I enter Ada and 3 it should print: AdaAdaAda Which loop correctly does this?

name = input("Enter your name: ")num = int (input("Enter a number: ")) c = 1 while (c <= num): print (name) c = c + 1

What is output by the following code? sum = 0for i in range (3, 15, 3):sum = sum + iprint ("Sum = " +str( sum))

Sum = 30

4.3 Code Practice: Question 1

x = int(input("How old are you turning? ")) c = 0 while (c != x): c = c + 1 print("**HUG**")

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

1 2 3 4 5

What is output by the following code? for x in range (3, 18): print (x - 2, end=" ")

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

What list of numbers is created by the following code? range(4, 11)

4 5 6 7 8 9 10

Consider the following code: val = 0 while (val < 10): val = val - 1 print (val) What is the error?

The loop will not stop since val is counting the wrong direction.

Which of the following is NOT a use of computer simulations:

To calculate the area of an oval room

A for loop is used to replace a ________ while loop.

counting

Assignment 4: Evens and Odds

n = int(input("How many numbers do you need to check? ")) odd = 0 even = 0 for i in range(1,n+1): num = int(input("Enter number: ")) if num%2 == 0: print(str(num)+" is an even number.") even = even + 1 else: print(str(num)+" is an odd number.") odd = odd + 1 print("You entered "+str(even)+" even number(s).") print("You entered "+str(odd)+" odd number(s).")

Ask the user to input a number less than 100. Print all the numbers from that number to 100. Which loop correctly does this?

num = int (input("Enter a number between 1 and 100: ")) c = num while (c <= 100): print (c) c = c + 1

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

Which range function will generate a list of all even numbers between 12 and 84 inclusive?

range (12, 86, 2)

Which range function creates the following list of numbers? 24 27 30 33 36 39

range(24, 42, 3)

Which range function will create a list with all odd numbers between 5 and 77?

range(5, 78, 2)

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


Set pelajaran terkait

Chapter 17 Lesson 4 Geography Lebanon, Syria, & Jordan

View Set

Ch 5 - Humerus and Shoulder Girdle

View Set

Ap Environmental Science: Forestry

View Set

14: Interest Groups (FINAL STUDY)

View Set

Prelude 5: Music as Passion and Individualism

View Set

Soil Science Exam 1 Practice Questions

View Set

PRECIOUS METALS... Aluminum and other metals

View Set