EBK STARTING OUT WITH PYTHON
4th Edition
ISBN: 8220106714294
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 7, Problem 5MC
This will happen if you try to use an index that is out of range for a list
a. A valuError exception will occur.
b. An IndexError exception will occur.
c. The list will be erased and the
d. Nothing—the invalid index will be ignored.
Expert Solution & Answer
Learn your wayIncludes step-by-step video
schedule03:11
Students have asked these similar questions
If you call the index method to locate an item in a list and the item is not found, this happens.a. A ValueError exception is raised.b. An InvalidIndex exception is raised.c. The method returns −1.d. Nothing happens. The program continues running at the next statement.
python exercise:
Write a Python function called calculate_average that takes a list of numbers as input and returns the average of the numbers in the list. If the input list is empty, the function should raise an EmptyListError exception.
Handle the case where the input list is empty by raising a custom EmptyListError exception. This exception should have an error message that says "Cannot calculate average of an empty list".
This Phyton:
In this lab, you will write a program that reads integers from a file “data.txt” into a list, print the list and the sum of the numbers in the list. The file contains one integer per line. You are not required to do any exception handling. Your program should not prompt the user for any input. The input file is “data.txt”.
First, create a file “data.txt” and put the numbers add the numbers 3, 1, 5, 2 into it, one number per line as shown in the sample “data.txt” file below.
Sample “data.txt” file
3
1
5
2
Then, write the following functions:
read_numbers(filename)
Read numbers from the given file and return a list of contains the numbers from the file.
compute_sum(numbers)
Compute the sum of the given the list numbers. Do not use the built-in sum function.
main()
Read numbers from the file “data.txt”, print the list, and compute the sum and print it. Main will use the two functions above to get the job done.
Sample run:
[3, 1, 5,…
Chapter 7 Solutions
EBK STARTING OUT WITH PYTHON
Ch. 7.2 - What will the following code display? numbers =...Ch. 7.2 - Prob. 2CPCh. 7.2 - Prob. 3CPCh. 7.2 - Prob. 4CPCh. 7.2 - Prob. 5CPCh. 7.2 - Prob. 6CPCh. 7.2 - Prob. 7CPCh. 7.2 - Prob. 8CPCh. 7.3 - Prob. 9CPCh. 7.3 - Prob. 10CP
Ch. 7.3 - Prob. 11CPCh. 7.3 - Prob. 12CPCh. 7.3 - Prob. 13CPCh. 7.4 - What will the following code display? names =...Ch. 7.5 - Prob. 15CPCh. 7.5 - Prob. 16CPCh. 7.5 - Prob. 17CPCh. 7.5 - Prob. 18CPCh. 7.8 - Prob. 19CPCh. 7.8 - Prob. 20CPCh. 7.8 - Write a set of nested loops that display the...Ch. 7.9 - Prob. 22CPCh. 7.9 - Prob. 23CPCh. 7.9 - Prob. 24CPCh. 7.9 - Prob. 25CPCh. 7.10 - Prob. 26CPCh. 7.10 - Prob. 27CPCh. 7.10 - Prob. 28CPCh. 7.10 - Prob. 29CPCh. 7.10 - Prob. 30CPCh. 7.10 - To create a bar chart with the bar function, what...Ch. 7.10 - Assume the following statement calls the bar...Ch. 7.10 - Prob. 33CPCh. 7 - This term refers to an individual item in a list....Ch. 7 - This is a number that identifies an item in a...Ch. 7 - Prob. 3MCCh. 7 - This is the last index in a list. a. 1 b. 99 c. 0...Ch. 7 - This will happen if you try to use an index that...Ch. 7 - This function returns the length of a list. a....Ch. 7 - When the operator's left operand is a list and...Ch. 7 - This list method adds an item to the end of an...Ch. 7 - This removes an item at a specific index in a...Ch. 7 - Prob. 10MCCh. 7 - If you call the index method to locate an item in...Ch. 7 - Prob. 12MCCh. 7 - This file object method returns a list containing...Ch. 7 - Which of the following statement creates a tuple?...Ch. 7 - Prob. 1TFCh. 7 - Prob. 2TFCh. 7 - Prob. 3TFCh. 7 - Prob. 4TFCh. 7 - A file object's writelines method automatically...Ch. 7 - You can use the + operator to concatenate two...Ch. 7 - Prob. 7TFCh. 7 - You can remove an element from a tuple by calling...Ch. 7 - Prob. 1SACh. 7 - Prob. 2SACh. 7 - What will the following code display? values = [2,...Ch. 7 - Prob. 4SACh. 7 - Prob. 5SACh. 7 - Prob. 6SACh. 7 - Prob. 1AWCh. 7 - Prob. 2AWCh. 7 - Prob. 3AWCh. 7 - Prob. 4AWCh. 7 - Write a function that accepts a list as an...Ch. 7 - Prob. 6AWCh. 7 - Prob. 7AWCh. 7 - Prob. 8AWCh. 7 - Total Sales Design a program that asks the user to...Ch. 7 - Prob. 2PECh. 7 - Rainfall Statistics Design a program that lets the...Ch. 7 - Prob. 4PECh. 7 - Prob. 5PECh. 7 - Larger Than n In a program, write a function that...Ch. 7 - Drivers License Exam The local driver s license...Ch. 7 - Name Search If you have downloaded the source code...Ch. 7 - Prob. 9PECh. 7 - World Series Champions If you have downloaded the...Ch. 7 - Prob. 11PECh. 7 - Prob. 12PECh. 7 - Magic 8 Ball Write a program that simulates a...Ch. 7 - Expense Pie Chart Create a text file that contains...Ch. 7 - 1994 Weekly Gas Graph In the student sample...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
An interpreter is a program that both translates and executes the instructions in a high-level language program...
Starting out with Visual C# (4th Edition)
For each of the following activities, give a PEAS description of the task environment and characterize it in te...
Artificial Intelligence: A Modern Approach
Write a do-whi1e loop that asks the user to enter two numbers. The numbers should be added and the sum displaye...
Starting Out with Java: From Control Structures through Objects (6th Edition)
Using examples, illustrate the different types of adaptor needed to support sequential composition, hierarchica...
Software Engineering (10th Edition)
Car Loan If A dollars are borrowed at r interest compounded monthly to purchase a car with monthly payments for...
Introduction To Programming Using Visual Basic (11th Edition)
(Frequency of characters) Write a program that prompts the user to enter the name of an ASCII text file and dis...
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
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
- Task 6 Given a list named my_list. my_list=[10,20,30,40,60,100,2,5] Write a python program that takes the position as an input from the user and prints the element of that position from the list. If the user's input position is not in the range, return an exception stating "Index out of range". Make sure your program always prints "Program ended" at the end, no matter what happens. Hint: Use finally block Example 1: Input: 1 Output: 20 Program ended Example 2: Input: 10 Output: Index out of range. Program endedarrow_forwardMust show it in Python: Please show step by step with comments. Please show it in simplest form. Input and Output must match with the Question Please go through the Question very carefully.arrow_forwardInvalid indexes in slicing expressions can produce an ‘out of bound’ exception.True or Flasearrow_forward
- If d is an empty dict in python, what is the name of the exception that will be thrown if you try to access d[1] ? ValueError NameError IndexError KeyErrorarrow_forwardA Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has these properties:-A grid that contains the numbers 1-9.-The sum of each row, each column, and each diagonal all adding up to the same number.In Python create a program simulatimg the magic square using a two-dimensional list.Additional requirements: -It must throw an exception if any character other than 1 – 9 is selected.-The program needs to give an message explaining that wrong input has been provided.-The program should prompt the user until the condition has been satisfied. It should prompt the user to enter a number from 1-9.-The program can only use each number once.-When the program starts, it should explain how to play the game. Those who have never heard of it should be able to play. The directions should be clear.-Show an example of a Lo Shu Magic Square after each game.-The program should allow users to play the game until they choose to quit.It also must keep track of wins and…arrow_forwardWhat happens if an exception is thrown, but not caught?arrow_forward
- in pytrhon programming language, What happens when an unhandled exception occurs? The finally clause executes, and the code finishes. The finally clause executes, and the code returns -1. The finally clause executes, and then the exception is re-raised. The finally clause executes, and then runs the try block again.arrow_forwardWhat will happen if an exception is thrown but not caught?arrow_forwardUSING PYTHON: Given a list of 10 names, complete the program that outputs the name specified by the list index entered by the user. Use a try block to output the name and an except block to catch any IndexError. Output the message from the exception object if an IndexError is caught. Output the first element in the list if the invalid index is negative or the last element if the invalid index is positive. Note: Python allows using a negative index to access a list, as long as the magnitude of the index is smaller than the size of the list. Ex: If the input of the program is: 5 the program outputs: Name: Jane Ex: If the input of the program is: 12 the program outputs: Exception! list index out of range The closest name is: Johnny Ex: If the input of the program is: -2 the program outputs: Name: Tyrese Ex: If the input of the program is: -15 the program outputs: Exception! list index out of range The closest name is: Ryley CODE: names = ['Ryley', 'Edan', 'Reagan', 'Henry', 'Caius',…arrow_forward
- If an exception is raised and the program does not handle it with a try/exceptstatement, what happens?arrow_forwardA Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has these properties: -A grid that contains the numbers 1-9. -The sum of each row, each column, and each diagonal all adding up to the same number. In Python create a program simulatimg the magic square using a two-dimensional list. Additional details: -It must throw an exception if any character other than 1 – 9 is selected. -The program needs to give an message explaining that wrong input has been provided. -The program should prompt the user until the condition has been satisfied. It should prompt the user to enter a number from 1-9. -The program can only use each number once. -When the program starts, it should explain how to play the game. Users who have never heard of it should be able to play. The directions should be clear. -Show an example of a Lo Shu Magic Square after each game. -The program should allow users to play the game until they choose to quit. It also must keep track…arrow_forward1. Write a program that creates an array with 100 randomly chosen integers. Your program should prompt the user to enter an index of the array, then displays the corresponding element value. It should prompt he user to enter the index again if the specified index is out of bounds and display the message "Index is out of bounds". You must use Exceptions to handle "index is out of bounds" error. Hint: Use ArraylndexOutOfBoundsException 2. What are the advantages of using exception handling? Attach File Browse Local Filesarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Linux - Tutorial for Beginners in 13 MINUTES! [ UPDATED ]; Author: bai;https://www.youtube.com/watch?v=BMGixkvJ-6w;License: Standard YouTube License, CC-BY
What is Linux?; Author: Techquickie;https://www.youtube.com/watch?v=zA3vmx0GaO8;License: Standard YouTube License, CC-BY
Introduction to Linux and Basic Linux Commands for Beginners; Author: sakitech;https://www.youtube.com/watch?v=IVquJh3DXUA;License: Standard Youtube License