Question & Answer: using Python 3.1…..

using Python 3.1

Attached Files:

File test_taxes.py (1.45 KB)

File taxes.csv (62 B)

File unit_test1.py (710 B)

File unit_test3.py (557 B)

File unit_test2.py (535 B)

Objectives:

The purpose of this homework assignment is to demonstrate your ability to apply what you have learned about format strings, modular programming and writing text files.

Instructions:

You are to create a module that I can import into my program that performs according to specification. Here’s what the module will do:

1. The first program will be named get_state_sales_tax().

this function doesn’t have any parameters, but does return a dictionary

the function reads a file named taxes.csv, attached, and splits the data into state and sales tax rate multiplier

the state serves as the key and tax rate multiplier as the value for each key-value pair in the dictionary

note that the name of the function MUST BE named get_state_sales_tax()

2. The second program will be named get_state_total(s, p, r)

The first parameter is a string and contains the name of a state

The second is a floating point value representing a price

The third is a floating point value representing a sales tax multiplier

The purpose of the function is to return a string suitable for display that includes the state, the price and the total price, including sales tax

The string should look like this

New Jersey (price / with tax): $1.95 / $2.09

where s is “New Jersey”, p is 1.95 and r is 1.07

The field width for the state must be 15 characters wide with blank fill so that the dollar signs all line up (refer to the video)

3. The third program is named log_it(m) and logs a message, m, appending it to the end of a text file.

The log file must be named tax_log.txt

The old messages must be preserved, meaning the file gets longer and longer each time log_it(m) is called.

Remember to open and close the file at the start and end of the function

Remember to append a new line to each message when writing the file

Note that all files must be in the same directory. That includes the test programs, your module and the CSV file.
Resources:

Here’s a video demonstrating how your program should behave and what the display should look like:

unit_test1.py – This program tests just your taxes.get_state_sales_tax() function. Make no changes to unit_test1.py. Just run it.
unit_test2.py – This program tests just your taxes.get_state_total() function. Make no changes to unit_test2.py. Just run it.
unit_test3.py – This program tests your taxes.log_it() function. Make no changes to unit_test3.py. Just run it.

test_taxes.py – System test program. You run this program to test that all of the functions in your module work. You do not make any changes to this file. You just run it.

 

Expert Answer

 

Note: Please place the all .py and .csv files in a same folder.

1.

Program screenshot:

Sample input file screenshot:

Sample output screenshot:

Code to copy:

# import csv module

import csv

# Create a funciton named “get_state_sales_tax”.

# This function is used to read the data from the

# file named “taxes.csv” and stored in dictonary.

def get_state_sales_tax():

with open(“taxes.csv”,”r”) as data_file:

# Read and split the file to lines

lines = data_file.read().split(‘n’)

# Create a while loop to add the data in a dictonary

while ” in lines:

# The following line is used to remove the empty

# line in a data

lines.remove(”)

# Dictionary to hold the required data

sales_tax_dict = dict()

# Create a “for” loop to read each line and

# assign in a dictonary.

for each_line in lines:

state, tax_rate = each_line.split(‘,’)

sales_tax_dict[state] = float(tax_rate)

# Return dictonary

return sales_tax_dict

# The following statements are used to test the function

# named “get_state_sales_tax”.

# Call the function

data=get_state_sales_tax()

# Display the data in the dictonary.

for state, tax in data.items():

print(state, tax)

———————————————————————————

2.

Program screenshot:

Sample ouput screenshot:

Code to copy:

# Create a funciton named “get_state_total”.

# This function is used to display a string which

# contains state, price and total price including sales tax.

def get_state_total(s,p,r):

info=s[:15]+”(price / with tax): $”+str(round(p,2))+”/$”+ str(round((p*r),2))

#information = f'{s:15s}(Price/with tax):${p:.2f}/${p*r:.2f}’

return info

# The following statements are used to check

# the function named “get_state_total”.

print(get_state_total(‘New Jersey’,1.95,1.07))

—————————————————————————

3.

Program screenshot:

Sample output screenshot:

Code to copy:

# Create a function named “””.

# This funciton is used to append a message

# in tax_log.txt file.

def log_it(m):

# Open the file in append mode

with open(“tax_log.txt”,”a”) as log_data_file:

# Add a newline character to our message

m += “n”

# Write the message in a file.

log_data_file.write(m)

# Close the file

log_data_file.close();

# The following programs are used to text

# function.

log_it(“Hello how are you?”)

log_it(“This is a second message.”)

Still stressed from student homework?
Get quality assistance from academic writers!