Question: Specification: Write a script using Python3 The script must define 4 functions random_number_file……

Specification:

Write a script using Python3

Don't use plagiarized sources. Get Your Custom Essay on
Question: Specification: Write a script using Python3 The script must define 4 functions random_number_file……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

The script must define 4 functions

random_number_file_create

lines_print

lines_count

total_numbers_in_file

random_number_file_create

This functions must have the following header

    def random_number_file_create(min, max, filename, entries):

This function must create a file where each line has a pseudorandom number.
Each of these numbers must be integers between the values of the parameters min and max.
The name of the file is given by the parameter filename.
The number of lines in the file is given by the parameter entries.
The function must close the file after it has written all the values.

lines_print

This functions must have the following header

    def lines_print(filename):

This function must print each line of the file.
There should be no blank lines between the lines of the file.
The function must close the file after it has printed all the lines.

lines_count

This functions must have the following header

    def lines_count(filename):

This function must count the number of lines in the file and return that total.
The function must close the file after it has read all the lines.

total_numbers_in_file

This functions must have the following header

    def total_numbers_in_file(filename):

This function must calculate the total of all the numbers in the file and return that total.
The function must close the file after it has read all the lines.

Suggestions:

Create the file hw8.py.
Import the random module.
Create the header for each of the functions.
For the body of each of the functions, use pass.
Run the script and fix any errors you find.

Remove the pass statement from random_number_file_create.
Replace it with a statement that creates a file object for writing for the filename given by the parameter.
Add the following function call to the bottom of the file

random_number_file_create(50, 100, 'numbers.txt', 20)

Run the script and fix any errors you find.

Add a for loop to random_number_file_create that will run for as the number of times specified by the parameter entries.
Inside the loop print the loop variable.
Run the script and fix any errors you find.

Inside the for loop, remove the statement that prints the loop variable. Replace this statement with one that creates a random integer between min and max and assigns it to a variable.
Add a statement to print this variable.
Run the script and fix any errors you find.

Inside the for loop, replace the statement that prints the loop variable with one that writes this value to the file.
Remember to turn the value into a string and to follow it with the linefeed character n.
Run the script, and run cat on the file numbers.txt.
Fix any errors you find.

Replace the pass statement in lines_print with a statement that creates a file object for reading on the filename given by the parameter filename.
Use a loop to print the lines in the file. Remove the call to random_number_file_create that you entered earlier.
Add the first 5 lines of the test code to the bottom of the file. Run the script and fix any errors you find.

Replace the pass statement in lines_count with a statement that creates a file object for reading on the filename given by the parameter filename.
Set a accumulator variable to 0. Write a loop that loops through the lines in the file.
Do not print the lines but increment the accumulator for each line in the file.
Return the accumulator value.
Add the 6th and 7th lines in the test code to the bottom of the file.
Run the script and fix any errors you find.

Replace the pass statement in total_numbers_in_file with a statement that creates a file object for reading on the filename given by the parameter filename.
Set a accumulator variable to 0.
Write a loop that loops through the lines in the file.
Inside the loop, convert each line into an integer and add it to the accumulator. Return the accumulator value.
Add the remainder of the test code.
Run the script and fix any errors you find.

Test Code

At the bottom of the script, you must have the following code

FILENAME = "numbers.txt"
random.seed(83)
random_number_file_create(50, 100, FILENAME, 20)
lines_print(FILENAME)
print()
entries = lines_count(FILENAME)
print("Entries:", entries)
total   = total_numbers_in_file(FILENAME)
average = round(total/entries)
print("Total:", total)
print("Average:", average)

The code above will run the functions you create, and make it easier for me to score this assignment.

Testing:

When you run your script the output should look exactly like this

81
79
55
58
74
52
53
91
63
62
61
69
55
53
76
59
72
69
85
62

Entries: 20
Total: 1329
Average: 66

Expert Answer

answers

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. Make sure that you maintain proper indentations while pasting the below code into your program, I’m also attaching the code in both text and image formats, so in case if the indentation is messed up, you can refer the image and maintain proper spacing in each and every line. Thanks

If you are satisfied with the solution, please rate the answer. If not, let me know before you rate the answer, I’ll correct anything you want.

#code

import random #importing required module

#method to create the file
def random_number_file_create(min, max, filename, entries):
#opening file in write + mode
 file=open(filename,‘w+’)
#looping for entries number of times
 for i in range(entries):
#generating a number between min and max
 num=random.randint(min,max)
#writing to file, adding a line break
 file.write(str(num)+n)
#closing file once done
 file.close()

#method to print all lines in the file
def lines_print(filename):
#opening file
 file=open(filename)
#looping through all lines
 for line in file:
#printing line after removing newline character
 print(line.strip())
file.close() #closing file once done

#method to count number of lines in a file
def lines_count(filename):
#opening file
 file=open(filename)
#reading all lines into a list
 lines=file.readlines()
#closing file
 file.close()
#returning length of list, which will be the number of lines
 return len(lines)

#method to find the sum total of all values in a file
def total_numbers_in_file(filename):
#opening file
 file=open(filename)
total=0 #defining a variable
#looping through file
 for line in file:
#adding to total the int value of line
 total+=int(line.strip())
#closing file
 file.close()
#returning total
 return total

#script to test the methods
FILENAME = “numbers.txt”
random.seed(83)
random_number_file_create(50, 100, FILENAME, 20)
lines_print(FILENAME)
print()
entries = lines_count(FILENAME)
print(“Entries:”, entries)
total   = total_numbers_in_file(FILENAME)
average = round(total/entries)
print(“Total:”, total)
print(“Average:”, average)

#code screenshot

1 import random #importing required module 3 #method to create the file 4 def random_number_file_create(min, max, filename, entries): #Opening tile In write mode file-open(filename, +) #looping for entries number of times 8for i in range(entries): #generating a number between min and max num-random.randint (min,max) #writing to file, adding a line break file.urite(str(num)+n) 9 18 12 13 #closing file once done file.close() 15 16 #method to print all lines in the file 17 def lines_print(filename): 18 19 20 #opening file file-open (filename) #looping through all lines for line in file: #printing line after removing newline character print(line.strip()) 23 24 file. close() #c losing file once done 26 #method to count number of lines in a file 27-det lines-count(filename) : #opening file file-open(filename) #reading all lines into a list lines-file.readlines() #c losing file file.close() #returning length of list, which will be the number of lines return len(lines) 29 30 32 35 37 #method to find the sum total of all values in a file 38 def total_numbers_in file(filename): #opening file file-open(filename) total-e #defining a variable #looping through file for line in file: 48 42 43 #adding to total the int value of line total+-int(line.strip()) 45 46 47 48 49 50 51 #script to test the methods 52 FILENAME numbers.txt 53 random.seed (83) 54 random_number file_create (50, 100, FILENAME, 20) 55 lines_print(FILENAME) 56 print() 57 entries-lines. count(FILENAME) 58 print( Entries:, entries) 59 totaltal_numbers_in_file(FILENAME) 68 average=round(total/entries) 61 print(Total:, total) 62 print(Average:, average) #c losing file file.close() #returning total return total

#output

81

79

55

58

74

52

53

91

63

62

61

69

55

53

76

59

72

69

85

62

Entries: 20

Total: 1329

Average: 66

Top Grade Homework
Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents