Unit 4: Computer Programming
What is output by the following code? for x in range (4): for y in range (3): print("*", end=" ") print("")
* * * * * * * * * * * *
When do you use a "for" loop instead of a "while" loop? (There may be more than one answer.) (2 answers)
-You know how many times you want the loop to run. -When there is a defined start and end
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
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 (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 then13?
18
What is output? Select all that apply. c = 6 while (c > 0): print (c) c = c - 2
2, 4, 6
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.
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):
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: 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 rule for algorithms?
They repeat indefinitely.
Which loop prints the numbers 1, 2...100?
c = 1 while (c <= 100): print (c) c = c + 1
For a loop to stop, the loop control variable must ______________.
change inside the loop
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: Ada Ada Ada 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
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
Information sent to a function is a?
parameter
Which range function will generate a list of all EVEN numbers between 12 and 84 inclusive?
range (12, 86, 2)
In a(n) __________________ loop, you do not know ahead of time how many times the loop will repeat.
user input
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