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...
One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56")....
One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56"). stringstream ss("23,4,56"); char ch; int a, b, c; ss >> a >> ch >> b >> ch >> c; // a = 23, b = 4, c = 56 You have to complete the function vector parseInts(string str). str will be a string consisting of comma-separated integers, and you have to return a vector of int representing the integers. Note If you want to...
File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program...
File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program to compute the summation of these 100,000 numbers using single thread. Write a C++ program to compute the summation of these 100,000 numbers using 10 threads, meaning each thread compute 10,000 summations and the main thread sum the result of the 10 threads. Using the time syscall to compare the time spent for 1.1 and 1.2 Turn Ins: Source code of two programs and...
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.
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
Java How to read a comma separated value file using Scanner for example Scanner sc =...
Java How to read a comma separated value file using Scanner for example Scanner sc = new Scanner(filename); I need to assign each value separated by a comma to different variable types. I need a good example to know how it works and implement it in my project Please only use Scanner to read the file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT