Question

In: Computer Science

USE BASIC PYTHON Focus on Basic file operations, exception handling. Save a copy of the file...

USE BASIC PYTHON

Focus on Basic file operations, exception handling.

Save a copy of the file module6data.txt (Below)

Write a program that will

a. Open the file module6data.txt

b. Create a second file named processed. txt

c. Read the numbers from the first file one at a time. For each number write into second file, if possible, its

I. Square

ii. Square root

iii. Reciprocal (1/number)use a math module function for the square root. use exception handling to trap possible errors.

d. Report the number of items in the original file and the number that were successfully stored in the second file.

e. Report the number of each type of error: ValueError, ZeroDivisionError, TypeError

26

O

76

-91

85

-44

85

-95

-34

83

-64

-76

-41

89

0

83

-99

-69

8

-79

0

-32

-49

-89

85

79

-28

56

19

-93

-21

-23

82

51

-80

l

62

-78

-8

71

28

-73

0

-45

-73

28

-5

O

-70

63

-36

-72

0

-76

-24

-59

0

-54

83

35

-3

88

-14

29

-35

17

27

-61

-42

49

83

38

79

-80

33

8

41

96

-94

46

71

8

76

-63

-36

93

-82

-68

-20

69

-57

-84

-29

-17

77

36

-89

-94

62

10

-69

-43

  • Use meaningful variable names.  Single letter variable names are forbidden except as counters in loops.
  • Use blank lines to separate major parts of a program or function.
  • Use spaces around all operators. For example, write x = y + 5, NOT x=y+5
  • Use named constants, not magic numbers.  
  • Organize your python program file. Your file should have, in this order
    • a full header comment, as specified in the guidelines
    • any named constants used by the program,
    • the definition of the main() function,
    • all functions other than main(),  
    • the call to main()at the very bottom of the file.

Solutions

Expert Solution

RAW CODE

import math

File = open('module6data.txt', 'r') ### Opening File
Lines = File.readlines() ## Reading file line by line
output = open('processed. txt', 'w') ### Opening new file processed.txt
original_count, processed_count = 0, 0 ### Initializing values to 0
ve,te,zde = 0,0,0
for line in Lines:
        try:
                original_count += 1 ### x += 1 is same as x = x + 1
                square, square_root, reciprocal = int(line) ** 2, math.sqrt(int(line)), 1 / int(line) ## math.sqrt() don't work on negative numbers
                number = int(line)
                output.write("{}: {}, {}, {}\n".format(number, square, square_root, reciprocal)) ## output is saved in format "number: square, square_root, reciprocal" in text file.
                processed_count += 1
        except ValueError: ### Type of error
                ve += 1
        except ZeroDivisionError:
                zde += 1
        except TypeError:
                te += 1

output.close() ### closing file

if __name__ == '__main__': ### main function.
        print("Number of items in the original file: ",original_count) ### Reporting results
        print("Number of items in the processed file: ",processed_count)

        print("Number of TypeError: ",te)
        print("Number of ValueError: ",ve)
        print("Number of ZeroDivisionError: ",zde)

SCREENSHOTS (CODE WITH OUTPUT)

processed.txt file

###### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. ######


Related Solutions

In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be...
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be in basic python. Part 1. Write a program that does the following: 1. Create a list of length N where N is a randomly selected integer between 10 and 20 and whose elements are randomly selected integers between 0 and 19. 2. Print out the list. 3. Create a copy of the list. Sort the copy into decreasing order and print it. 4. Report...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based methods c. onClick d. DDMS 2. What would this query return? SELECT * FROM Items WHERE OrderDate >= Date('Now') a. All items in the database. b. Nothing c. Items that were ordered yesterday d. Items that were ordered today. 3. What can you use to manage files within a desired directory and create subdirectories? a. Subdirectory objects b. Directory class c. File objects d....
Python 3 Code does not save properly the data in the excel file. it stores the...
Python 3 Code does not save properly the data in the excel file. it stores the data in the same row over and over # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT