Question

In: Computer Science

Starting out with Python Files and Exceptions - Random Number Writer, Reader, and Exception Handler In...

Starting out with Python

Files and Exceptions - Random Number Writer, Reader, and Exception Handler

In this assignment, you will be writing a series of random numbers to a file, reading the file, and handling exceptions.

Begin by writing a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 100. The application should let the user specify how many random numbers the file will hold.

Next, write code that will read the random numbers from the file, display the numbers, then display the following data:

  • The total of the numbers
  • The number of random numbers read from the file

Finally, modify the program so it handles the following exceptions:

  • Any IOError exceptions that are raised when the file is opened and data is read from it.
  • Any ValueError exceptions that are raised when the items that are read from the file are converted to a number.

This should all be completed within ONE python scr

Solutions

Expert Solution

code:

# importing random package to create random numbers
import random

# Taking input from the user
x = int(input("How many random numbers you want to store: "))

# opening file in writing mode
f = open("random.txt","w")

# for loop to create x random numers
for i in range(x):
n = random.randint(1,100) # This method will create random integers in range 1 through 100
f.write(str(n)) # converting n to string and writing to file
f.write("\n") # To write every generated random number in a new line
f.close() # closing file

# Handling IOError exception while reading file
try:
f1 = open("random.txt","r") # now opening file in reading mode
print("The numbers in the file are: ")
print(f1.read()) # read() method will read all lines in file

f1.seek(0) # moving file cursor to initial position
s = c = 0 # Initially sum s and count c will be 0

# iterating through every line in file
for num in f1:

# Handling ValueError exception
try:
s+=int(num) # converting string to integer and sum it to s
except ValueError:
print("Not a valid number")
c+=1 # incrementing count
  
# Printing statements
print("\nThe total of numbers is: ",s)
print("Number of random numbers read from file are: ",c)
except IOError:
print("No such file or directory")

OUTPUT

Output for IOError: Here i gave incorrect file name


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...
Starting out with python Lists and Tuples - US Population Data In this assignment, you will...
Starting out with python Lists and Tuples - US Population Data In this assignment, you will be reading the contents of a file into a list. This file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the populations for 1951, and so forth. You will ask the user to input a specific year to check the population...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you...
Create a java random program from example (1,2,3,4,5,6,7,8,9,10) But do exception for number 7 when you run the program first time for example it will print 3 then when you run the program for the second time it will give a random number for example 9 but number 7 will not be printed because we need you to do exception for it.
“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words...
“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words some ways functions are beneficial to the programmer. In other words, why might a programmer want to use functions?
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
Write a Python program which generates a random number between 0 and 24 inclusive and that...
Write a Python program which generates a random number between 0 and 24 inclusive and that print out: • the double of the random number if the random number is greater than 12, • the triple of the random number if it is equal to 12 • and the quadruple of the random number if it is less than 12
continuous random variable and uniform distribution please follow the comment. A random number generator spits out...
continuous random variable and uniform distribution please follow the comment. A random number generator spits out a random real number in the range [1,4] assume each number is equally likely being out. what is the probability that the model output an irrational number? the answer is 1, but i don't understand
The first random number generator comes from Python itself. To use it you need to import...
The first random number generator comes from Python itself. To use it you need to import the Python package. Then call the random() method. See below. import random print (random.random()) It is important to know that a random number generator really is random. If so, it should have uniform distribution between 0 and 1. Test the random number generators in Python for uniformity.
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT