CSCI 102 - Intro to Computer Science LAB

Python Assignment: Week 06

For Loops

Quick Links: zyBook | AutoGrader | Piazza | Canvas | CS @ Mines

Home | Contact | Schedule | Assignments | Syllabus |

Week 06 Python Assignment
Due by Tuesday, October 5th, 2021
        Part A: List of Multiples (must (1) demo during class (2) upload to Gradescope by 11:45pm)
        Part B: Estimating Square Roots (due to Gradescope by 11:45pm)


Welcome to your assignment for Week 06 of CSCI 102! Each week, you will typically have multiple homework assignments (labs). These labs are to be done on your own (not pair programming), with one lab to be demoed in class Tuesday. All labs (including the demo labs) need to be submitted to Gradescope 11:45pm on the due date. Unlike CSCI 101, we will place all assignments for a given week on one HTML page. This is your Week 6 CSCI 102 Assignment page.

You have TWO problems to do this week, the first of which (Part A) you will demo in class with a TA on Tuesday. Details on Part A and Part B are below.

Part A: List of Multiples (3 points)

Introduction

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week6A-list_multiples.py. In previous labs, we've discussed looping using while. In this lab we will further our knowledge of loops by using for loops.

Problem Statement

Create a program that reads two numbers from the user, num and max_index, and uses a for loop to create a list of multiples of num until the list length reaches max_index + 1. See the sample executions below for examples.

Lab I/O Format

For lab assignments this semester, a specific Lab Input/Output Format is required. This format is described below:
  • When prompting for input, use the prompt string WORD>, where WORD is a single, uppercase word which describes the input. For example, this lab might choose: NUMBER> .
  • When providing output that will be graded, start the line with the word OUTPUT followed by exactly one space, e.g. OUTPUT . Think of this as "boxing your answer" on a math worksheet; it lets us quickly find your answer. Gradescope will skip any output lines that do not start with OUTPUT.
  • You are welcome to have other output lines that do not begin with OUTPUT; Gradescope will ignore these.
  • A submission without exactly correct output formatting will receive an AUTOMATIC ZERO. This is because Gradescope is automated—it does not look at your code, only the results, and thus the format of the results must be consistent for all students.

Example Execution #1

Enter the number to create multiples for.
NUMBER> 5
Enter the maximum index of the list.
INDEX> 6
Your list of multiples is as follows:
OUTPUT [5, 10, 15, 20, 25, 30, 35]

Example Execution #2

Enter the number to create multiples for.
NUMBER> 13
Enter the maximum index of the list.
INDEX> 4
Your list of multiples is as follows:
OUTPUT [13, 26, 39, 52, 65]

Gradescope Submission Nuances

You will demo this lab in class on Tuesday and should also submit it to Gradescope after demoing in class. Because this is a demo lab, no tests will be run upon submission to Gradescope—your grade will be based on your performance in the demo.

Comments

All Python files should include a header with your name, section, assignment info, references (i.e., who did you collaborate with on this assignment?; what resource did you use?), and approximate time taken to do the assignment. . Be sure to cite any allowed external references used to complete the assignment. Any code without this header will lose 1 point. Here's an example:
        #   John Henke
        #   ​CSCI 102 – Section B
        #   Week 6 - Lab A - List of Multiples
        #   References: Instructor Eric for for loop syntax and TA Tori for list creation
        #   Time: 20 minutes

Part B: Estimating Square Roots (4 points)

Introduction

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week6B-square_roots.py.

Problem Statement

Oh no! John was taking a Physics 2 Exam and his calculator died! He remembers how to perform basic operations by hand, but he never learned how to estimate square roots. Help John perform square roots manually so he can pass his physics exam.

The method we will use to estimate the square root of a given number, num, is as follows:
  1. Guess a positive number, we will call it initial_guess. For consistency, you MUST initialize initial_guess to 10 when estimating each square root or your output will be incorrect!
  2. Apply the formula better_guess = (initial_guess + num / initial_guess) / 2 .
  3. If initial_guess equals better_guess, then we have converged to our answer. Otherwise, initial_guess becomes better_guess and we repeat steps 2 and 3 until convergence.
John will tell you how many square roots he needs you to estimate. Your program should use a for loop to read in each number and a while loop to estimate the square root of each number. Estimate the square root of each number using the method above, keeping track of the number of iterations before the formula converges (this is why your initial_guess MUST always start as 10). Output both the number of iterations (as an integer) and the estimate (to three decimal places). See the examples below for proper output formatting. Your solution's output should exactly match the examples given below.

Reminder: it is best to use a for loop when we know how many times to iterate, and a while loop if we do NOT know how many times to iterate. You should use both loop types in this lab.

Hint: we encourage you to develop a solution to this lab in pseudocode first, and then do incremental development.

Lab I/O Format

For lab assignments this semester, a specific Lab Input/Output Format is required. This format is described below:
  • When prompting for input, use the prompt string WORD>, where WORD is a single, uppercase word which describes the input. For example, this lab might choose: NUMBER> .
  • When providing output that will be graded, start the line with the word OUTPUT followed by exactly one space, e.g. OUTPUT . Think of this as "boxing your answer" on a math worksheet; it lets us quickly find your answer. Gradescope will skip any output lines that do not start with OUTPUT.
  • You are welcome to have other output lines that do not begin with OUTPUT; Gradescope will ignore these.
  • A submission without exactly correct output formatting will receive an AUTOMATIC ZERO. This is because Gradescope is automated—it does not look at your code, only the results, and thus the format of the results must be consistent for all students.

Example Execution #1

How many numbers am I estimating John?
COUNT> 5
Input each number to estimate.
NUMBER> 897.5
NUMBER> 4728898745
NUMBER> 55.55
NUMBER> 100
NUMBER> 1
The square roots are as follows:
OUTPUT After 7 iterations, 897.5^0.5 = 29.958
OUTPUT After 18 iterations, 4728898745.0^0.5 = 68766.989
OUTPUT After 6 iterations, 55.55^0.5 = 7.453
OUTPUT After 1 iterations, 100.0^0.5 = 10.000
OUTPUT After 9 iterations, 1.0^0.5 = 1.000

Example Execution #2

How many numbers am I estimating John?
COUNT> 3
Input each number to estimate.
NUMBER> 3
NUMBER> 12321
NUMBER> 74618764
The square roots are as follows:
OUTPUT After 8 iterations, 3.0^0.5 = 1.732
OUTPUT After 9 iterations, 12321.0^0.5 = 111.000
OUTPUT After 16 iterations, 74618764.0^0.5 = 8638.215

Gradescope Submission Nuances

Part B will ONLY be submitted to Gradescope (you will NOT demo this lab in class).
When you submit your Python file to Gradescope, multiple different test cases are run on your code. Passing all of the tests results in a 100% on the autograded portion of the lab.

You are allowed to submit to Gradescope four times (or less) for this lab. The maximum grade of your submissions will be your grade for the lab. Note: If your code doesn’t work (e.g., a syntax error exists, or an error is thrown in execution), then you will received an AUTOMATIC ZERO. You should test your code before submitting to ensure it executes correctly.

Comments

All Python files should include a header with your name, section, assignment info, references (i.e., who did you collaborate with on this assignment?; what resource did you use?), and approximate time taken to do the assignment. Be sure to cite any allowed external references used to complete the assignment. Any code without this header will lose 1 point. Here's an example:
        #   John Henke
        #   ​CSCI 102 – Section B
        #   Week 6 - Lab B - Estimating Square Roots
        #   References: TA Carter for pseudocode development
        #   Time: 50 minutes

Submit Solutions

Follow these steps to submit your files to Gradescope.
  1. In Gradescope, go to Assignments > Week6A and upload Week6A-list_multiples.py.
  2. In Gradescope, go to Assignments > Week6B and upload Week6B-square_roots.py.
To receive full credit, your code must execute in Python 3, and you must submit a single file for each portion of the assignment (your Python code file). In addition, your code must follow the Lab I/O Format.

Whenever you submit something to Gradescope, we strongly recommend you always double check what you submitted actually got submitted correctly (e.g., did the file upload correctly? did you submit the correct file? etc.) If your submission is incorrect, it's on you.