Python代写:CS10-Python-Programming-Homework5

CS10 Python Programming Homework 5

40 points

Dictionaries, Files, OOP

  1. You must submit to Canvas only your program listing and output for each program set. Each program set must have your student name, student ID, and program set number/description. Late homework will not be accepted for whatever reasons you may have.

**

for this homework, you are only to submit All Program Sets to Canvas under Homework 5 link


a. Name your files : HW5_PS1_lastname_firstinitial.py for Program Set 1 and so on. PS means program set.
b. You have till 11:59pm as shown on Canvas to submit all Program Sets to Canvas. If the deadline is past, Program Sets will not be graded.
c. if you do not follow instructions on file naming provided in this section you will receive a zero for the whole of Homework 5.

  1. Please format you output properly, for example all dollar amounts should be printed with 2 decimal places.
    Make sure that your output values are correct (check the calculations).
  2. Each student is expected to do their own work. IF IDENTICAL PROGRAMS ARE SUBMITTED, EACH IDENTICAL PROGRAM WILL RECEIVE A SCORE OF ZERO.

Grading:

Each program set must run correctly syntactically, logically, and display the correct output as specified. If the program set does not run correctly, a zero will be given. For each Program set, if the program executes properly with proper syntax, logic, and displays the correct output, then points will be deducted for not having proper: a. Comments (1 pt deducted for each occurrence)

  • Your name, description at the beginning of each program set. Short description of the what each section of your codes do.
    b. Consistency/Readability (2 pts deducted for each occurrence)
  • Spacing(separate each section of codes with a blank line
  • Indentation
  • Style (proper naming of variables no a,b,c – use descriptive and mnemonics)
    -each function must include type hints or annotations except for the main()
    -include docstrings for every function
    c. Required elements (2 pts deducted for each occurrence)
  • Use tools that have been covered in class
  • proper formatting for output when specified
  • all monetary values must be in 2 decimal places
    d. Output
  • if no output is provided for either the hardcopies or uploaded file, a zero will be given for that program set.
  • Output must to be displayed at the end of the program listing(codes), if it not displayed at the end of the program listing it is equivalent to no output.
  • must use test cases when provided in the Program set question. Provide your own test cases if the program set does not ask for any. The minimum test cases you provide on your own is 5 or more. If you provide less then 5 test cases per Program Set then that program set will receive a zero grade.

Program 1- Tea Leaves Sales Report (15 Points)

  1. Here is the sample of the text file (datafile) for the tea sales (tea.txt). The tea.txt file is provided for you on Canvas.
1
2
3
4
5
Green Tea:8580.0:7201.25:8900.
Earl Grey:10225.25:9025.0:9505.
Ceylon:6700.1:5012.45:6011.
Jasmine:9285.15:8276.1:8705.
Mint Tea:7901.25:4267.0:7056.
1
The data reads as follows – tea_name:store1_Sales:store2_Sales:store3_Sales
1
The output displayed as follows(no commas required for the numbers in this output)

>>>

1
2
3
4
5
6
Ceylon 6700.10 5012.45 6011.00 17723.
Earl Grey 10225.25 9025.00 9505.00 28755.
Green Tea 8580.00 7201.25 8900.00 24681.
Jasmine 9 285.15 8276.10 8705.00 26266.
Mint Tea 7901.25 4267.00 7056.50 19224.
42691.75 33781.80 40177.

>>>

1
2
The last row is the store totals for all the kinds of tea leaves sold. The last column is the total sale for
each kind of tea leaves sales. Note that the tea names are sorted.
1
2
a. Write a program to read in the tea.txt textfile and then print the report as shown in #2 above.
b. You must use dictionary. You can also use list, tuples, sets if necessary.

Program Set 2(15 points)

  1. The textabbrv.txt file is provided for you on Canvas. You are to use this text file and write and program as shown in the output sample. Allow the user to run the program as many times as the user wants. You must provide at least 5 test cases.
1
2
a. You are to write a program where the user enters an abbreviation and your program will translate it to
English.
1
2
b. your program should take into consideration all punctuation marks that the user enters and displayed it
in the English translation.
1
c. You must use dictionary. You may use list, tuples, sets if you need to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Below is the textabbv.txt file contents(use the textabbv.txt file provided for your program):
r:are
u:you
ttyl:talk to you later
l8:late
brb:be right back
lol:laughing out loud
bbl:be back later
tldr:too long; didn't read
rofl:rolling on floor laughing
gtg:got to go
cya:see you
cuzz:because
bff:best friend forever
1
The output should display:

>>>

1
2
3
4
5
Enter a message to be translated: (prompt for the user)
r u , lol? (user input the abbreviation)
The translated text is: (your program translates and prints the next 2 lines including
are you , laughing out loud? punctuations)
>>>

Program 3 – OOP (10 points)

Write a program to calculate and display the loan for buying a car.

  1. Create a class call Loan.
    Data fields in the Loan class include:
    1 Annual Interest Rate (Float)
  2. Number of years of loan (Float)
  3. Loan Amount (Float)
  4. Borrower’s Name (string)
  5. Create the initializer or constructor for the class with the above data fields. Make the data fields private.
  6. Create accessors (getter) for all the data fields.
  7. Create mutators (setters) for all the data fields.
  8. Create a class method - getMonthlyPayment where
1
monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / (1 + monthlyInterestRate) ** (numberOfYears * 12)))
1
note: that the monthly interest rate = annualinterest / 1200
  1. Create a class method - getTotalPayment where

totalPayment = getMonthlyPayment() numberOfYears 12

  1. Write a test program (main function) to allow the user to enter the following:
  2. Annual Interest Rate
  3. Number of Years of Loan
  4. Loan Amount
  5. Borrower’s Name

Allow the user to change the loan amount and reprint the new loan information.

The output should look like this:

>>>

Enter yearly interest rate: 2.

Enter number of years as an integer: 5

Enter loan amount: 1000.

Enter a borrower’s name: John Jones

The loan is for John Jones

The monthly payment is 17.

The total payment is 1,064.

Do you want to change the loan amount? Y for yes or enter to quit y

Enter new loan amount 5000

The loan is for John Jones

The monthly payment is 88.

The total payment is 5,324.

>>>