Question

1.Write a python program that writes a series of random numbers to a file named random.txt....

1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold.

2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data:

I. The total of the numbers

II. The number of random numbers read from the file

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

Source code:

pr1.py

import random
f = open("random.txt", "a")
rn = input("How many random numbers you want to enter in file ? ")
rn = int(rn)           # convert string to int

for x in range(rn):
    i = random.randint(1,301)
    i = str(i)           # convert int to string
    f.write(i +'\n')       # write to file line by line


prg2.py

f = open("random.txt", "r")
sumOfNums = 0
count = 0

for num in f:
   print(num)
   sumOfNums = sumOfNums + int(num)       # convert string to int and add it to sumOfNums
   count = count + 1               # increment count
  
print("Number of random numbers: %d\n" %count)
print("Sum of random numbers : %d\n" %sumOfNums)

Output:

Activities Terminal Sat 21:35 root@misomaniac-desktop: File Edit View Search Terminal Help prg2 . ру root@misonaniac-desktop># python prgl.py How many random numbers you want to enter in file? 10 root@misonaniac-desktop : ~# cat randon.txt 247 256 272 247 123 235 27 201 289 只-174 294 236 160 215 148 root@misonaniac-desktop ; ~# 247 python prg2.py 256 272 247 123 235 27 201 289

Add a comment
Know the answer?
Add Answer to:
1.Write a python program that writes a series of random numbers to a file named random.txt....
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
  • Please use Python for this program Random Number File Writer Write a program that writes a...

    Please use Python for this program 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 1 through 500. The program should let the user specify how many random numbers to put into the file. The name of the file to write to should be 'random.txt'. Submit the random.txt file generated by your program with the assignment. Sample program execution: Python 3.4.3 Shell Eile Edit...

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

  • This assignment assumes you have completed Programming Assignment 6, Random Number File Writer. Write another program...

    This assignment assumes you have completed Programming Assignment 6, Random Number File Writer. Write another program that reads the random numbers from the random.txt file created in Programming Assignment 6, display the numbers, and then display the following data: The total of the numbers. The number of numbers read from the file. Please program in python

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

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

  • a file containing a series of integers is named numbers.txt. Write a Python program that calculates...

    a file containing a series of integers is named numbers.txt. Write a Python program that calculates the average of all the numbers stored in the file. Sample Run 49.6

  • PYTHON write a program that will open a file named data.txt and read each line of...

    PYTHON write a program that will open a file named data.txt and read each line of the file one at a time. Each line should be printed to the screen along with a line number

  • In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

    In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file...

  • Random Number File Reader This exercise assumes you have completed Programming Problem 13, Random Number File...

    Random Number File Reader This exercise assumes you have completed Programming Problem 13, Random Number File Writer . Create another application that uses an OpenFileDialog control to let the user select the file that was created by the application that you wrote for Problem 13. This application should read the numbers from the file, display the numbers in a ListBox control, and then display the following data: • The total of the numbers • The number of random numbers read...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

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