Question

In: Computer Science

The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write...

The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g.

2.0,1
5.5,2
10,0
5.1,9.6

  • Write a program which reads the file using the Python CSV module, creates a list from the contents, then divides the first number in each line by the second.
  • Define a function in your program that performs the division operations; the function must accept two numeric parameters, divide the first parameter by the second, and return the result. Before dividing, your function must validate the second parameter; if it is zero, raise a ZeroDivisionError exception which includes the string "denominator is zero!" as a parameter instead of processing the division operation. Do not display any output in your function.
  • Define a second function which reads the CSV file and returns the list of number pairs.
  • Your main function must call the file reader function, then process the list by calling your divide function repeatedly and displaying the results (include the contents of the raised exception object, if applicable).

Expected Output:

2.0 divided by 1.0 is 2.0
5.5 divided by 2.0 is 2.75
10.0 divided by 0.0 error: denominator is zero!
5.1 divided by 9.6 is 0.53125

What is the file name I should save this under for the CSV file to run properly I keep getting an error when trying to run the code?

Solutions

Expert Solution

check out the solution.

-------------------------------------------------------

CODE :

# Python program - readCSVDemo.py
import csv

# divide function implementation
def divide(n1, n2):
# raise an exception
if(n2 == 0):
raise ZeroDivisionError("denominator is zero!")
  
# perform division and return result
result = n1/n2
return result

def read():
lst = [] # initialize a list
# open csv file with read mode
with open('numberpairs.csv','r')as f:
data = csv.reader(f) # read file contents
for row in data:
lst.append(row) # append each row to list
  
return lst # return the list


# main function
def main():
# function call
lst = read()
  
for line in lst:
# as divide function raise an exception, use try-except block
try:
# get each number from each line and convert it into float
n1 = float(line[0])
n2 = float(line[1])
result = divide(n1, n2) # function call
print(f'{n1} is divided by {n2} is {result}') # print result
except ZeroDivisionError as z: # exceptions are handled here
print(f'{n1} is divided by {n2} error: {z}') # print accordingly
  
# main calls
if __name__ == "__main__":
main()

-------------------------------------------------------------------------

---------------------

OUTPUT :

================================


Related Solutions

assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
(1) Prompt the user for a string that contains two strings separated by a comma.
(In C)(1) Prompt the user for a string that contains two strings separated by a comma. Examples of strings that can be accepted:Jill, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. Ex:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string and remove any spaces. Store...
*Python Stock Exchange Data Problem Expected Duration: 3-6 hours A comma-separated value file stocks_data.csv contains historical...
*Python Stock Exchange Data Problem Expected Duration: 3-6 hours A comma-separated value file stocks_data.csv contains historical data on the Adjusted Closing Price for 3 stocks daily from Jan 2, 2009 to Dec 31,2018. The stocks are Apple (APPL), Microsoft (MSFT) and IBM (IBM). A snippet from the data file looks like the following: Symbol Date Adj. Close MSFT 4/16/2014 35.807358 MSFT 6/21/2010 20.752356 IBM 2/10/2009 68.930023 AAPL 2/14/2018 164.227203 IBM 6/13/2017 141.24379 IBM 12/26/2017 142.835663 MSFT 4/1/2009 15.053272 AAPL 4/17/2009...
(1) Prompt the user for a string that contains two strings separated by a comma. (1...
(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex: Enter input...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by...
A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Write a program that reads such a file and displays the total amount for each service category. Use the following data for your text file:5 pts Bob;Dinner;10.00;January 1, 2013 Tom;Dinner;14.00;January 2, 2013 Anne;Lodging;125.00;January 3, 2013 Jerry;Lodging;125.00;January...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT