For the code below, write a prediction on what it will output. Then run the code and state if your prediction was accurate or not. If your prediction is incorrect, make sure you understand why.
If you don't know why the code runs the way it does, watch the video at the end of the assignment for an explanation. If you are looking at the text-only version of this worksheet, go on-line and find the HTML version of this worksheet for the video.
This section is worth 10 points, a half point per problem rounded up.
for i in range(5): print(i + 1)
for i in range(5): print(i) i = i + 1
x = 0 for i in range(5): x += 1 print(x)
x = 0 for i in range(5): for j in range(5): x += 1 print(x)
for i in range(5): for j in range(5): print(i, j)
for i in range(5): for j in range(5): print("*", end="") print()
for i in range(5): for j in range(5): print("*", end="") print()
for i in range(5): for j in range(5): print("*", end="") print()
# This is supposed to sum a list of numbers # What is the mistake here? my_list = [5, 8, 10, 4, 5] i = 0 for i in my_list: i = i + my_list[i] print(i)
for i in range(5): x = 0 for j in range(5): x += 1 print(x)
import random play_again = "y" while play_again == "y": for i in range(5): print(random.randrange(2), end="") print() play_again = input("Play again? ") print("Bye!")
def f1(x): print(x) y = 3 f1(y)
def f2(x): x = x + 1 print(x) y = 3 f2(y) print(y)
def f3(x): x = x + 1 print(x) x = 3 f3(x) print(x)
def f4(x): z = x + 1 print(z) x = 3 f4(x) print(z)
def foo(x): x = x + 1 print("x=", x) x = 10 print("x=", x) foo(x) print("x=", x)
def f(): print("f start") g() h() print("f end") def g(): print("g start") h() print("g end") def h(): print("h") f()
def foo(): x = 3 print("foo has been called") x = 10 print("x=", x) foo() print("x=", x)
def a(x): print("a", x) x = x + 1 print("a", x) x = 1 print("main", x) a(x) print("main", x) def b(y): print("b", y[1]) y[1] = y[1] + 1 print("b", y[1]) y=[123, 5] print("main", y[1]) b(y) print("main", y[1]) def c(y): print("c", y[1]) y = [101, 102] print("c", y[1]) y = [123, 5] print("main", y[1]) c(y) print("main", y[1])
This next section involves finding the mistakes in the code. If you can't find the mistake, check out the video at the end for the answer and an explanation on what is wrong.
This section is worth 7 points.
def sum(a, b, c): print(a + b + c) print(sum(10, 11, 12))
def increase(x): return x + 1 x = 10 print("X is", x, " I will now increase x." ) increase(x) print("X is now", x)
def print_hello: print("Hello") print_hello()
def count_to_ten(): for i in range[10]: print(i) count_to_ten()
def sum_list(list): for i in list: sum = i return sum list = [45, 2, 10, -5, 100] print(sum_list(list))
def reverse(text): result = "" text_length = len(text) for i in range(text_length): result = result + text[i * -1] return result text = "Programming is the coolest thing ever." print(reverse(text))
def get_user_choice(): while True: command = input("Command: ") if command = f or command = m or command = s or command = d or command = q: return command print("Hey, that's not a command. Here are your options:" ) print("f - Full speed ahead") print("m - Moderate speed") print("s - Status") print("d - Drink") print("q - Quit") user_command = get_user_choice() print("You entered:", user_command)
(13 pts) For this section, write code that satisfies the following items: