In this assignment, you will read and debug some python programs. You may not use any Python interpreter (e.g., Thonny) in completing this assignment, we strongly recommend that you treat this assignment as a practise take home exam to help you prepare for your upcoming midterm. You may use your course notes, past assignments and labs.
Begin by downloading the hmwk4.txt starter file (right/secondary click on this link, then select “Download Linked File As…” or “Save Linked As…”), and save it locally. You MUST use this starter file to get credit for the assignment.
The following Python program contains four common programming errors:
1 instruction = input("STARS or STRIPES? ")
2 num = input("How many? ")
3
4 if (instruction == "STARS"):
5 print("*" * num)
6 else if (instruction == "STRIPES"):
7 print("=" + num)
8 else
9 print("Not a valid choice.")
For each error that you find, write its line number and a brief description of the problem (you may refer to the slides from Friday of Week 3).
Consider the following Python program:
1 def main():
2 n = int(input("Choose any integer > 0: "))
3 while (n > 1):
4 if (n % 2 == 0):
5 n = n // 2
6 else:
7 n = n*3 + 1
8 print(n)
9
10 main()
Q2. (a)-(d), for each of the following scenarios, what is the output of the program (or what Exception is thrown).
Q2. (a) The user enters the value 9
.
Q2. (b) The user enters the value -8
.
Q2. (c) The user enters the value "four"
.
Q2. (d) The user enters a value of your choice (specify value).
Q2. (e) Describe in your own words (i.e., in 1-3 English sentences) what the program computes (not how it does it). Note: If you have seen this program (or its purpose) in another class, reference this class/source in your answer.
In this step you will write a prgram that adds the first 15 terms of the series: 1, 4, 9, 16, 25, 36, …
Notice: each term is a squared number: 1*1, 2*2, 3*3, 4*4, 5*5, 6*6, ...
Q3. (a) Explain in one English sentence, what is the goal of this program (i.e., what does it compute).
Q3. (b) What concepts do you need to use in order to answer this question. See page 9 of the course pack for examples of concepts covered in class.
Q3. (c) Write out the steps to your algorithm in bullet form (in English).
Q3. (d) Write out the Python code for your algorithm (i.e., your solution to the original question).
Consider the following Python program:
import math
1 def main():
2 factor = 2
3 my_bool = True
4 num = int(input("Enter a positive integer: "))
5 factor_max = math.floor(math.sqrt(num))
6 while (factor <= factor_max and my_bool):
7 if (num % factor == 0):
8 my_bool = False
9 else:
10 factor = factor + 1
11 print (my_bool)
12 main()
Q4. (a) and (b), for each of the following scenarios, what is the output of the program (or what Exception is thrown).
Q4. (a) The user enters the value 11
.
Q4. (b) The user enters the value 8
.
Q4. (c) What are all the possible outputs of this program, assuming the user enters a valid input?
Q4. (d) Describe in your own words (i.e., in 1-3 English sentences) what the program computes (not how it does it). Note: If you have seen this program (or its purpose) in another class, reference this class/source in your answer.
Your submission must include the course header at the top.
Deadline: February 25, 2019 at 10:00PM (EST). Go to our Moodle page. Submit your hmwk4.txt
file and fill out your self-assessment.