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
  • in python im trying to get this to print to the file i it created def...

    in python im trying to get this to print to the file i it created def calculateTotal(): filename = input("Enter a name for the file : ")#ask users for a name for file no_of_students = int(input("Enter the number of students in class : ")) data = [] with open(filename,"w+") as f: for index in range(no_of_students): grades = input("Enter Name of student plus grade :").split() # store the student data in array "data" data.append(grades) # store the total grades in "total"...

  • 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...

  • I need help writing python code with following instructions. You will write a program that reads...

    I need help writing python code with following instructions. You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exactly (or better than) the one attached (output.txt). Input: A31 149.99 B31 49.99 A41 179.99 F31 169.99 A35 179.99 A44 169.99 open "input.txt" file using open()...

  • 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...

  • Write a function that takes, as an argument, the name of a file, fileName . Your...

    Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...

  • 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 =...

  • 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...

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