Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony 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.
In Python:
If you call the index method to locate an item in a list and the item is not found, this happens
A ValueError exception is thrown
An InvalidIndex exception is thrown
The method returns -1
Nothing happens, The method does not return anything and the program continues execution
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
Starting Out with Python (4th Edition)
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
What is the importance of modeling in engineering? How are the mathematical models for engineering processes pr...
HEAT+MASS TRANSFER:FUND.+APPL.
State whether each of the following is true or false. A set of statements contained within a pair of parenthese...
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
What populates the Smalltalk world?
Concepts Of Programming Languages
Write a summary list of the problem-solving steps identified in the chapter, using your own words.
BASIC BIOMECHANICS
_________ is when member functions in a class hierarchy behave differently, depending upon which object perform...
Starting Out with C++ from Control Structures to Objects (9th Edition)
This key word indicates that a class inherits from another class. a. derived b. specialized c. based d. extends
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
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_forwardIf t is a list with exactly one element in python, what is the name of the exception that will be thrown if you try to access t[1]? NameError IndexError ValueError KeyErrorarrow_forward
- Invalid indexes in slicing expressions can produce an ‘out of bound’ exception.True or Flasearrow_forwardIf 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_forward
- What happens if an exception is thrown, but not caught?arrow_forwardin 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_forward
- USING 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_forwardIf an exception is raised and the program does not handle it with a try/exceptstatement, what happens?arrow_forwardWrite a python program that takes two lists as input from the user, multiplies the index-wise elements of the two lists, and adds it to a new variable. While doing the program you should handle the following exceptions: Return an exception if an element in a list is not a number stating “The list has some non-number values”. Return an exception if there is an index out of bound stating “Index out of bound”. ============================= Example 1:Input:list_one = [1, a, 2, 4]list_two =[5, 6, 7, 8]Output:The list has some non number values ================= Example 2:Input:list_one=[1, 2, 3, 4, 5]list_two=[1, 2, 3, 4]Output:Index out of bound ================= Example 3:Input:list_one=[1,2,3,4]list_two=[5,6,7,8]Output:70arrow_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