Question

Create a Word doc file (name of the file: numbers) using Python. Write the following numbers...

Create a Word doc file (name of the file: numbers) using Python. Write the following numbers to the Word document as shown below and read these numbers from the file and calculate and print the sum and average of these numbers.

9

5

7

6

3

After that, read these values from the file and calculate the sum and average of these numbers.  

I have wrote this so far, but doesnt work!

def numbersfile():
f = open("numbers.docx, "w")
for i in range(10):
n = str(random.randint(1, 10))
f.write(n)
f.write('\n')
f.close()
fx = open("numbers.docx","r")
print("5 digits produced from random int numbers.txt file:\n")
for i in range(1,6):
line1 = fx.readline()
line2 = int(line1)
print(line2,end=' ')

fx.close

numbersfile()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import random

f_w = open("numbers.docx", 'w', encoding="ISO-8859-1")

for i in range(5):
   n = str(random.randint(1, 10))
  
   f_w.write(n)
   f_w.write('\n')

f_w.close()

f_r = open("numbers.docx", 'r', encoding="ISO-8859-1")

sum_num = 0
count = 0

print("\nNumbers in the file are :")
for line in f_r:
   n = int(line)
   print(n)
  
   sum_num = sum_num + n
   count = count + 1

print("\nSum of numbers :", sum_num)
print("Average of numbers :", sum_num/count)

print("\n")

Execution and Output:

Add a comment
Know the answer?
Add Answer to:
Create a Word doc file (name of the file: numbers) using Python. Write the following numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • C++ need #7 and #8 6. Write code that does the following: Opens an output file...

    C++ need #7 and #8 6. Write code that does the following: Opens an output file with the filename Numbers.txt, uses a loop to write the numbers 1 through 100 to the file, and then closes the file. ofstream outputFile("Numbers.txt"); for(int number = 1; number <= 100; number++) outputFile<< number << endl; outputFile.close(); 7. Write code that does the following: Opens the Numbers.txt file that was created by the code you wrote in question 6, reads all of the numbers...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • In Python... To automatically shut down the machine for 5 seconds then restart it, if the...

    In Python... To automatically shut down the machine for 5 seconds then restart it, if the temperature reaches 100 degrees or more. Write a program that reads in the temp_readings.txt file. It should read each temperature and display it. If the temperature is greater than 100 degrees it should display "Shutting down the machine", then it should display "Cooling" five times. Then it should display "Starting the machine" and read in the next temperature from the file. Below temp_readings.txt file...

  • HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE? from itertools import accumulate from collections import Counter...

    HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE? from itertools import accumulate from collections import Counter def addvalues(a, b): if type(a) is str: a = a.strip("\n") if type(b) is str: b = b.strip("\n") return int(a) + int(b) def maxValues(a, b): if type(a) is str: a = a.strip("\n") if type(b) is str: b = b.strip("\n") a = int(a) b = int(b) return max(a, b) def main(): counter = 0 total = 0 maximum_cars = 0 file = open("MainAndState.dat", "r") maximum =...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • Is there another way to write this code in python? def processFile(): inFile = open("inputFile.txt") sumOfNumbers...

    Is there another way to write this code in python? def processFile(): inFile = open("inputFile.txt") sumOfNumbers = 0 count = 0 minimum = 0 maximum = 0 for line in inFile: n = int(line) if count == 0: minimum = n maximum = n else: if n > maximum: maximum = n if n < minimum: minimum = n count = count + 1 sumOfNumbers = sumOfNumbers + n inFile.close() avg = (int)(sumOfNumbers / count) avg = sumOfNumbers / count...

  • Use C++ Read numbers from a file and find the sum of numbers. Perform the following...

    Use C++ Read numbers from a file and find the sum of numbers. Perform the following operations Read the console input and create a file.txt. $ in the console input is considered as an end of content for a file. Close the file after creation. Open the file in a read mode and read the numbers from a file and print the sum of all numbers. Input 77 124 2333 $          Where $ indicate an end of console input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT