A document from MCS 260 Fall 2021, instructor David Dumas. You can also get the notebook file.

# MCS 260 Fall 2021 Homework 6¶

• Course instructor: David Dumas

## Instructions:¶

• Complete the problems below, which ask you to write Python scripts.
• Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 5, 2021.

### Topic¶

This homework assignment focuses on:

• Command line arguments
• Opening, Reading and writing files
• String formatting

### Collaboration¶

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

### Resources you may consult¶

The course materials you may refer to for this homework are:

### Point distribution¶

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
4 Problem 2
4 Problem 3
10 Total

### What to do if you're stuck¶

Ask your instructor or TA a question by email, in office hours, or on discord.

## ( 1. There's no problem 1 )¶

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

## 2. Company renaming¶

Suppose a company called NetShareTube has decided to rename itself to SynerGrowthTech. Unfortunately, they have a lot of text files (Python program code, documentation, etc.) that will need to be updated because they refer to the old name.

Write a Python program hwk6prob2.py to assist with the renaming as follows:

• It accepts two command line arguments: an input filename and an output filename (in that order)
• The program opens the input file for reading
• The program opens the output file for writing
• The contents of the input file are read, any instance of NetShareTube is changed to SynerGrowthTech, and the result is written to the output file

For example, if a file mission_statement.txt in the current directory contains this text:

At NetShareTube, we are committed to excellence in all its forms.  We produce
scalable enterprise solutions for delivering excellent customer experiences.

NetShareTube was founded in 2019 by a visionary leader who is no longer with
the company.

and if the following command is run

python3 hwk6prob2.py mission_statement.txt mission_statement_updated.txt

then a new file mission_statement_updated.txt should be created with the following content:

At SynerGrowthTech, we are committed to excellence in all its forms.  We produce
scalable enterprise solutions for delivering excellent customer experiences.

SynerGrowthTech was founded in 2019 by a visionary leader who is no longer with
the company.

## 3. Dice report¶

Write a program hwk6prob3.py that uses Python's random module to simulate rolling two 6-sided dice (each with the numbers 1, 2, 3, 4, 5, 6 on its faces) 20 times in a row. The program should make note of each time the dice roll totals to 7. The program should display the results on the screen as a table in the following format:

D1 D2 S?    SPct
2  5  Y  100.00
1  3  N   50.00
2  5  Y   66.67
2  6  N   50.00
2  1  N   40.00
2  3  N   33.33
1  4  N   28.57
2  6  N   25.00
4  1  N   22.22
6  6  N   20.00
4  4  N   18.18
3  5  N   16.67
2  1  N   15.38
1  4  N   14.29
1  6  Y   20.00
1  4  N   18.75
2  5  Y   23.53
4  3  Y   27.78
6  2  N   26.32
5  6  N   25.00

Here, column D1 shows the roll of the first die, D2 shows the roll of the second die, and S? shows Y or N to indicate whether or not the dice sum to 7. The final column SPct shows the percentage of rolls so far that sum to 7. Notice that the columns are nicely aligned, with the right edge of each column label matching the right hand side of the column of numbers or values. Also, percentages are shown with 2 digits after the decimal point (so, allowing for 3 digits before the decimal point and the point itself, each percentage is shown in a way that has a total width of 6 characters).

While you can certainly use the solution to the "coin flip report" from Worksheet 6 as a starting point for this problem, make note of a key difference:

• The coin flip report program writes its report to a file (displaying nothing on the screen)
• This program is supposed to write its report to the screen (not creating or using any files)

## Revision history¶

• 2021-09-30 Initial release