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.
Block 1 (Remember, guess AND actual. You'll lose 19 points if you skip
guessing the output of the next 19 problems, and completely miss the point
of this part.)
fori inrange(5):
print(i +1)
Block 2
fori inrange(5):
print(i)
i =i +1
Block 3
x =0
fori inrange(5):
x +=1
print(x)
Block 4
x =0
fori inrange(5):
forj inrange(5):
x +=1
print(x)
Block 5
fori inrange(5):
forj inrange(5):
print(i, j)
Block 6
fori inrange(5):
forj inrange(5):
print("*", end="")
print()
Block 7
fori inrange(5):
forj inrange(5):
print("*", end="")
print()
Block 8
fori inrange(5):
forj inrange(5):
print("*", end="")
print()
Block 9
# This is supposed to sum a list of numbers
# What is the mistake here?
my_list =[5, 8, 10, 4, 5]
i =0
fori inmy_list:
i =i +my_list[i]
print(i)
Block 10
fori inrange(5):
x =0
forj inrange(5):
x +=1
print(x)
Block 11
importrandom
play_again ="y"
whileplay_again =="y":
fori inrange(5):
print(random.randrange(2), end="")
print()
play_again =input("Play again? ")
print("Bye!")
Block 12
deff1(x):
print(x)
y =3
f1(y)
Block 13
deff2(x):
x =x +1
print(x)
y =3
f2(y)
print(y)
Block 14
deff3(x):
x =x +1
print(x)
x =3
f3(x)
print(x)
Block 15
deff4(x):
z =x +1
print(z)
x =3
f4(x)
print(z)
Block 16
deffoo(x):
x =x +1
print("x=", x)
x =10
print("x=", x)
foo(x)
print("x=", x)
Block 17
deff():
print("f start")
g()
h()
print("f end")
defg():
print("g start")
h()
print("g end")
defh():
print("h")
f()
Block 18
deffoo():
x =3
print("foo has been called")
x =10
print("x=", x)
foo()
print("x=", x)
Block 19 (This demonstrates a new concept that won't be fully explained until Chapter 12.)
defa(x):
print("a", x)
x =x +1
print("a", x)
x =1
print("main", x)
a(x)
print("main", x)
defb(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])
defc(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])
Video: Explanation of code blocks
Correcting Code
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.
Correct the following code: (Don't let it print out the word ``None'')
defsum(a, b, c):
print(a +b +c)
print(sum(10, 11, 12))
Correct the following code: (x should increase by one, but it doesn't.)
defincrease(x):
returnx +1
x =10
print("X is", x, " I will now increase x.")
increase(x)
print("X is now", x)
Correct the following code:
defprint_hello:
print("Hello")
print_hello()
Correct the following code:
defcount_to_ten():
fori inrange[10]:
print(i)
count_to_ten()
Correct the following code:
defsum_list(list):
fori inlist:
sum=i
returnsum
list=[45, 2, 10, -5, 100]
print(sum_list(list))
Correct the following code: (This almost reverses the string. What is wrong?)
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)
Video: Explanation of how to correct the code
(13 pts) For this section, write code that satisfies the following items:
Write a function that prints out ``Hello World.''
Write code that will call the function in the prior problem.
Write a function that prints out ``Hello Bob'', and will take a parameter to
let the caller specify the name. Do not put an input statement inside
the function! Use a parameter.
Write code that will call the function in the prior problem.
Write a function that will take two numbers as parameters (not as input from
the user) and print their product (i.e. multiply them).
Write code that will call the prior function.
Write a function that takes in two parameters. The first parameter will be a
string named phrase. The second parameter will be a number named
count. Print phrase to the screen count times.
(e.g., the function takes in "Hello" and 5, then prints "Hello" five times.)
Write code to call the previous function.
Write code for a function that takes in a number, and returns the square of
that number. (I'm not asking for the square root, but the number squared.)
Note, this function should RETURN the answer, not print it out.
Write code to call the function above and print the output.
Write a function that takes three numbers as parameters, and returns the
centrifugal force. The formula for centrifugal force is:
F=m(v^2/r)
F is force, m is mass, r is radius, and v is angular velocity.
Write code to call the function above and display the result.
Write a function that takes a list of numbers as a parameter, and prints out
each number individually using a for loop.