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...
I need to implement a code in python to guess a random number. The number must...
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate...
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?
Textbook: Starting out with Python (3rd or 4the Edition) Question: Programming Exercise # 9 - Trivia...
Textbook: Starting out with Python (3rd or 4the Edition) Question: Programming Exercise # 9 - Trivia Question In this programming exercise, you will create a simple trivia game for two players. The program will work like this: Starting with player 1, each player gets a turn at answering 5 trivia questions. (There should be a total of 10 questions.) When a question is displayed, 4 possible answers are also displayed. Only one of the answers is correct, and if the...
Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure...
Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure how to do this
Starting Out with Python 4th ed., Page 104 Programming exercise #14 When a bank account pays...
Starting Out with Python 4th ed., Page 104 Programming exercise #14 When a bank account pays compound interest, it pays interest not only on the principal amount that was deposited into the account, but also on the interest that has accumulated over time. Suppose you want to deposit some money into a savings account, and let the account earn compound interest for a certain number of years. The formula for calculating the balance of the account after a specified number...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT