Assignment 3
Tackle as many of these problems as you can, submitting your answers along the way via this Google Form, which should autosave your answers. Be sure to click SUBMIT at the bottom of the Google Form when you have completed the assignment. You should receive an email receipt when the form has been properly submitted. You can resubmit, too, as many times as you’d like before the assignment’s deadline. Refer to these instructions for guidance on submitting.
Please be mindful of the course’s policy on academic honesty as you complete this assignment. When you engage with others regarding this assignment in peer learning sessions, be aware of our guidelines for assignment-related discussions. With the above in mind, the solution for the first item below denoted with an asterisk (*) may be openly discussed without restriction during peer learning sessions.
Not to worry if you run into trouble. Do just attend office hours for help! Further, consult our AI-based tool, cs50.ai.
This term, we’ll introduce you (if not familiar already) to GitHub, a popular service with which to store code and collaborate with others.
So that you have an account, head to github.com/join and create an account (if you don’t have one already). Take care to remember your username and password! If you’d prefer not to provide GitHub with a personally identifiable username and email address (i.e., one that includes your name), you’re welcome to create an account using an anonymous username and some other email address (e.g., that you only use for GitHub). If you choose to provide a personally identifiable username or email address, material you submit for the course to GitHub and our feedback on it also will be personally identifiable by GitHub. See GitHub’s Privacy Statement at docs.github.com/en/github/site-policy/github-privacy-statement.
Then, log into code.cs50.io using your GitHub account in order to use Visual Studio Code (VS Code) in the cloud. No need to install anything on your own computer. Keep in mind that you can create or open a file (e.g., hello.py
) by typing
code hello.py
in VS Code’s terminal window. And you can run a progam (e.g., hello.py
) by typing
python hello.py
in VS Code’s terminal window.
Question 1*
Printing
Predict precisely what the following program will output when it is executed.
def main() :
print ("She said, \"", end="")
print ("Never put off till tomorrow... ", end="")
print ("\nWhat you can do")
print ("The day", end="")
print ()
print ("After tomorrow!\"")
main()
You may, of course, type this program into a file and see if the output matches your prediction.
Question 2
Understanding Flow of Control
Part 1
Precisely what does the following program output when we begin by executing main()
?
def first() :
print ("Inside first function!")
def second() :
first();
print ("Inside second function!")
def third() :
print ("Inside third function!")
first()
second()
def main() :
first()
third()
second()
third()
Part 2
What would have been the output of the preceding program, starting with main()
if the function third
had instead been written like this?
def third() :
first()
print("Inside third function!")
second()
Question 3
Understanding Assignment and Simple Arithmetic Operators
Without using a Python interpreter, what is the precise output from the following code?
bar = 13
foo = 23 - 45 // 10
foo += 6
bar = foo % bar
print (foo * 2)
print (foo + bar)
print (foo, bar)
Question 4
Practice with the For Loop and Range Function
Part 1
Complete the following Python code. You may only modify the indicated comment.
for j in range (5) :
# your code goes right here and nowhere else!
so that this loop will print the following numbers, one per line:
-2
1
4
7
10
Part 2
Produce this same sequence in a different way, replacing the question marks ONLY in the following code to output the same thing as in part 1:
for j in range ( ??? ) :
print (j)
Question 5
Computing a Value Based on User Input
To convert a temperature from Celsius degrees to Fahrenheit the following relationship holds:
\[F = \frac{9}{5} C + 32\]Write a Python program named temperature.py
that prints the Fahrenheit equivalent of any temperature, based upon the value of a numeric variable named celsius
that is input from the keyboard using the input()
function. Here is what your program might look like in action:
$ python temperature.py
Input a Celsius temperature to be converted: 100.0
100.0 degrees Celsius equals 212.0 degrees Fahrenheit
Question 6
Functions with Parameters that May or May Not Return Values
For each of the following, indicate whether the statement is true or false. If the statement is false, rewrite it so that it becomes true.
Part 1
Every function must have exactly one return statement.
Part 2
A function that does not return a value never will never have a return statement.
Part 3
A function that does not return a value could instead print a result.
Part 4
A function that uses parameters must use parameter names that are different from the names of the actual arguments that may be passed to the function.
Question 7
Random Numbers
Recall the function random()
in the random module generates a random float value uniformly in the semi-open range [0.0, 1.0) – in other words, the value of random()
will be >= 0.0 but strictly less than 1.0
Which one of the following expressions will yield a random integer value from 2 to 50 inclusive (in other words, including both 2 and 50 as possibilities)?
Assume in all cases we have imported the random module via from random import *
.
A. int (random() * 50) - 2
B. int (random() * 50) + 2
C. int (random() * 49) + 2
D. int (random() * 48) + 2
E. none of the above will work!
Question 8
Formatted and Unformatted Output
Which of the following code segments will print the message Catch 22
?
I. print ("Catch", 11 + 11)
II. print ("Catch " + (11 + 11))
III. print (f"Catch {11+11}")
A. I only
B. II only
C. III only
D. I and III only
E. II and III only
Question 9
Conditional Execution Using Boolean Expressions
Assume that both variables a
and b
are boolean-valued variables. Consider the following two assignment statements, which are executed sequentially:
a = a and b
b = a or b
Which of the following statements is/are true? You might want to consider making a “truth table” to help you figure out the answer.
I. The final value of a
is the same as the initial value of a
II. The final value of b
is the same as the initial value of b
III. The final value of a
is the same as the initial value of b
A. II only!
B. III only!
C. II and III only!
D. I and II only!
E. I only!