Modify Both python programs to handle IoError, ValueError and unspecified error expectations using Try and Except clauses

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Modify Both python programs to handle IoError, ValueError and unspecified error expectations using Try and Except clauses
```python
def readRandomNumbers():
    numbers = list()
    with open("random_numbers.txt", "r") as file:
        for line in file:
            numbers.append(int(line.rstrip()))
        file.close()
    return numbers

def displayData(numbers):
    print("The total of the numbers: %d" % sum(numbers))
    print("The numbers of random numbers read from the file: %d" % len(numbers))

if __name__ == "__main__":
    randomNumbers = readRandomNumbers()
    displayData(randomNumbers)
```

**Explanation:**

This Python script is designed to read numbers from a file and display some statistics about them. Here is a breakdown of the code:

- `readRandomNumbers()`: This function reads a file named `random_numbers.txt`. It assumes each line in this file contains a single integer. It opens the file, reads each line, strips any whitespace, converts it to an integer, and appends it to a list called `numbers`. After reading all lines, it returns the list of numbers.

- `displayData(numbers)`: This function takes a list of numbers as input and prints out two pieces of information: the sum of all the numbers in the list and the count of numbers read from the file.

- The `if __name__ == "__main__":` block ensures that the `readRandomNumbers()` and `displayData()` functions are called only when this script is executed directly, not when imported as a module.
Transcribed Image Text:```python def readRandomNumbers(): numbers = list() with open("random_numbers.txt", "r") as file: for line in file: numbers.append(int(line.rstrip())) file.close() return numbers def displayData(numbers): print("The total of the numbers: %d" % sum(numbers)) print("The numbers of random numbers read from the file: %d" % len(numbers)) if __name__ == "__main__": randomNumbers = readRandomNumbers() displayData(randomNumbers) ``` **Explanation:** This Python script is designed to read numbers from a file and display some statistics about them. Here is a breakdown of the code: - `readRandomNumbers()`: This function reads a file named `random_numbers.txt`. It assumes each line in this file contains a single integer. It opens the file, reads each line, strips any whitespace, converts it to an integer, and appends it to a list called `numbers`. After reading all lines, it returns the list of numbers. - `displayData(numbers)`: This function takes a list of numbers as input and prints out two pieces of information: the sum of all the numbers in the list and the count of numbers read from the file. - The `if __name__ == "__main__":` block ensures that the `readRandomNumbers()` and `displayData()` functions are called only when this script is executed directly, not when imported as a module.
```python
from random import randint

def generate_number():
    return randint(1, 500)

def write_to_file(count):
    with open("random numbers.txt", "w") as file:
        for i in range(count):
            num = str(generate_number())
            file.write(num + "\n")
    file.close()

if __name__ == "__main__":
    count = int(input("Enter Number of random numbers to generate: "))
    write_to_file(count)
    print("Written %d random numbers to file \"random numbers.txt\"." % count)
```

**Explanation:**

This Python script generates a specified number of random numbers between 1 and 500 and writes them to a file named "random numbers.txt". Here's a breakdown of its components:

1. **Import Statement:**
   - `from random import randint`: Imports the `randint` function from the `random` module, which generates random integers.

2. **Function Definitions:**
   - `generate_number()`: Returns a random number between 1 and 500.
   - `write_to_file(count)`: Takes an integer `count` and writes `count` random numbers to "random numbers.txt", one per line.

3. **Main Execution Block:**
   - Checks if the script is being executed directly.
   - Prompts the user for input to determine how many random numbers to generate.
   - Calls `write_to_file(count)` to generate and save the numbers.
   - Prints a confirmation message with the total numbers written to the file.
Transcribed Image Text:```python from random import randint def generate_number(): return randint(1, 500) def write_to_file(count): with open("random numbers.txt", "w") as file: for i in range(count): num = str(generate_number()) file.write(num + "\n") file.close() if __name__ == "__main__": count = int(input("Enter Number of random numbers to generate: ")) write_to_file(count) print("Written %d random numbers to file \"random numbers.txt\"." % count) ``` **Explanation:** This Python script generates a specified number of random numbers between 1 and 500 and writes them to a file named "random numbers.txt". Here's a breakdown of its components: 1. **Import Statement:** - `from random import randint`: Imports the `randint` function from the `random` module, which generates random integers. 2. **Function Definitions:** - `generate_number()`: Returns a random number between 1 and 500. - `write_to_file(count)`: Takes an integer `count` and writes `count` random numbers to "random numbers.txt", one per line. 3. **Main Execution Block:** - Checks if the script is being executed directly. - Prompts the user for input to determine how many random numbers to generate. - Calls `write_to_file(count)` to generate and save the numbers. - Prints a confirmation message with the total numbers written to the file.
Expert Solution
steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY