Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 6, Problem 9PE
Program Plan Intro
Exception Handling
Program Plan:
- • Declare a function named “main()”.
- ○ Declare the variable “amount” and assign “0.0” for this variable.
- ○ Declare the variable “value” and assign “0.0” for this variable.
- ○ Declare the variable “count” and assign “0”for this variable.
- ○ Declare a “try” suite.
- ○ Open the file named “numbers.txt” that is available in disk to read the numbers in the file and store the numbers in the variable “inputfile”.
- ○ Use the “for” loop to perform the iteration.
- ■ Increment “count” variable to count the iteration of the loop.
- ■ Convert the “integer” number stored in the variable “number” to “float” number and assign it to the variable “value”.
- ■ Add all the values and assign the total to the variable “amount”.
- ○ Close the file “numbers.txt”.
- ○ Calculate the average.
- ○ Display the average of the numbers stored in the variable “average”.
- ○ If the file named “numbers.txt” is not available in the disk, then it branches to the “IOError” except clause and print the exception handling statement.
- ○ If the file “numbers.txt” holds the “string” value, then it branches to the “ValueError” except clause and print the exception handling statement.
- ○ Rather than the above two exceptions, any other exception occurs in the “try” suite; then, it branches to the last “except” clause.
- ○ Call the function “ main()”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Pls help ASAP
A(n) ____ should be used to specify all the exception types when being handled by a single
exception handler.
list
O tuple
array
O dict
java programming language
note: thrown exceptions must be sent to txt file, not to console, it can be both, but it has to be txt file with the errors
Chapter 6 Solutions
Starting Out with Python (4th Edition)
Ch. 6.1 - What is an output file?Ch. 6.1 - What is an input file?Ch. 6.1 - What three steps must be taken by a program when...Ch. 6.1 - Prob. 4CPCh. 6.1 - Prob. 5CPCh. 6.1 - When writing a program that performs an operation...Ch. 6.1 - If a file already exists, what happens to it if...Ch. 6.1 - What is the purpose of opening a file?Ch. 6.1 - What is the purpose of closing a file?Ch. 6.1 - Prob. 10CP
Ch. 6.1 - In what mode do you open a file if you want to...Ch. 6.2 - Write a short program that uses a for loop to...Ch. 6.2 - Prob. 13CPCh. 6.2 - Assume the file data.txt exists and contains...Ch. 6.2 - Prob. 15CPCh. 6.3 - Prob. 16CPCh. 6.3 - Prob. 17CPCh. 6.3 - Prob. 18CPCh. 6.4 - Prob. 19CPCh. 6.4 - Prob. 20CPCh. 6.4 - What type of exception does a program raise when...Ch. 6.4 - Prob. 22CPCh. 6 - A file that data is written to is known as...Ch. 6 - A file that data is written to is known as...Ch. 6 - Before a file can be used by a program, it must be...Ch. 6 - When a program is finished using a file, it should...Ch. 6 - The contents of this type of file can be viewed in...Ch. 6 - This type of file contains data that has not been...Ch. 6 - When working with this type of file, you access...Ch. 6 - When working with this type of file, you can jump...Ch. 6 - This is a small holding section" in memory that...Ch. 6 - This marks the location of the next item that will...Ch. 6 - When a file is opened in this mode, data will be...Ch. 6 - This is a single piece of data within a record. a....Ch. 6 - Prob. 13MCCh. 6 - Prob. 14MCCh. 6 - Prob. 15MCCh. 6 - When working with a sequential access file, you...Ch. 6 - When you open a file that file already exists on...Ch. 6 - The process of opening a file is only necessary...Ch. 6 - Prob. 4TFCh. 6 - When a file that already exists is opened in...Ch. 6 - Prob. 6TFCh. 6 - You can have more than one except clause in a...Ch. 6 - Prob. 8TFCh. 6 - Prob. 9TFCh. 6 - Describe the three steps that must be taken when a...Ch. 6 - Why should a program close a file when it's...Ch. 6 - What is a read position? where is the read...Ch. 6 - If an existing file is opened in append mode, What...Ch. 6 - If a file does not exist and a program attempts to...Ch. 6 - Write a program that opens an output file with the...Ch. 6 - Write a program that opens the my_name.txt file...Ch. 6 - Write code that does the following: opens an...Ch. 6 - Prob. 4AWCh. 6 - Modify the code that you wrote in problem 4 so it...Ch. 6 - Write code that opens an output file with the...Ch. 6 - A file exists on the disk named students. txt. The...Ch. 6 - A file exists on the disk named students txt. The...Ch. 6 - Prob. 9AWCh. 6 - Prob. 10AWCh. 6 - File Display Assume a file containing a series of...Ch. 6 - File Head Display Write a program that asks the...Ch. 6 - Line Numbers Write a program that asks the user...Ch. 6 - Item Counter Assume a file containing a series of...Ch. 6 - Sum of Numbers Assume a file containing a series...Ch. 6 - Average of Numbers Assume a file containing a...Ch. 6 - Random Number File Writer Write a program that...Ch. 6 - Random Number File Reader This exercise assumes...Ch. 6 - Prob. 9PECh. 6 - Golf Scores The Springfork Amateur Golf Club has a...Ch. 6 - Personal Web Page Generator Write a program that...Ch. 6 - Average Steps Taken A Personal Fitness Tracker is...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- If you do not handle an exception, it is ignored by the Python interpreter, and the program continues to execute.True or Falsearrow_forwardWhen an exception is thrown, what kind of statement is used?arrow_forwardException handling and File I/O coding intellj Exercise 3. File I/O Please create a file and add the following line: 1,5,7,9,13,58,70 Exercise 4, Assuming you have a file on your laptop, and it has the following: John,23 Please create a Person object from your main method by reading the file. You have to create Person class (String name, int age) Please read the file and show the sum of all the numbers.arrow_forward
- When an exception occurs in a program, the code can deal with it in one of three ways: ignore it handle it where it occurs handle it an another place in the program If the code handles it where it occurs, what java construct is used? while loop try/catch another construct (not listed here) if statementarrow_forwardHow can an exception be thrown manually by a programmer?arrow_forwardjava programming language note: this is only one question, not threearrow_forward
- To throw an exception, what form of statement is used?arrow_forwardd) Handle the file while reading with an exception best suited.arrow_forwardPARTICIPATION ACTIVITY Arrange the following lines to make a program that determines when the number of people in a restaurant equals or exceeds 10 occupants. The program continually gets the number of people entering or leaving the restaurant. Ex: 2 means two people entered, and -3 means three people left. After each input, the program outputs the number of people in the restaurant. Once the number of people in the restaurant equals or exceeds 10, the program exits. If an Input MismatchException exception occurs, the program should get and discard a single string from input. Ex: The input "2 abc 8" should result in 10 occupants. Not all lines are used in the solution. How to use this tool ✓ Unused totalNumPeople += scnr.nextInt (); while (totalNumPeople < maxNumPeople) { } } 11.1.5: Handling input exceptions: restaurant max occupancy tracker. catch { try { catch (InputMismatchException e) { scnr .nextLine(); scnr.next(); System.out.println("Error"); } System.out.println("Occupancy:…arrow_forward
- Briefly describe what an exception is.arrow_forwardC2- Write a Python program that reads the contents of a file named ‘Book.txt’ (you must write exception handling if the file doesn’t exist) The program should also determine and print the following: The number of words in the file The number of distinct words (without repetition) The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file The number of whitespace characters in the filearrow_forwardWhich type of statment is used to throw an exception?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT