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
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,…
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
KeyError
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
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
- Write a program that reads the contents of the two files into two separate lists. The user should be able to enter a boy’s name, a girl’s name, or both, and the application will display messages indicating whether the names were among the most popular. your program should read the files into separate lists for boys and girls names the program should accept either a girl name or a boy name or both, entry of a NULL i.e. just pressing the "enter key" results in no search for that entry the program must continue to run until told to stop by the person running the program using any method of your choice report on the success or not of each attempt to locate a name in the popular names filesarrow_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_forwardIn a program you can stimulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu Magic Square. Test the function in a program. It must be coded using python. It takes the numbers in the list as input from the user.arrow_forward
- PYTHON: A pedometer treats walking 2,000 steps as walking 1 mile. Write a steps_to_miles() function that takes the number of steps as a parameter and returns the miles walked. The steps_to_miles() function throws a ValueError object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() program that reads the number of steps from a user, calls the steps_to_miles() function, and outputs the returned value from the steps_to_miles() function. Use a try-except block to catch any ValueError object thrown by the steps_to_miles() function and output the exception message. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:print('{:.2f}'.format(your_value)) Ex: If the input of the program is: 5345 the output of the program is: 2.67 Ex: If the input of the program is: -3850 the output of the program is: Exception: Negative step count entered.arrow_forwardI need only python output very quicklyarrow_forwardWhat is a throw point? 2. What is an exception handler? 3. Explain the difference between a try block and a catch block. 4. What happens if an exception is thrown, but not caught? 5. Complete the following function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. int searchArray(int array[], int size, int value){ int index = 0; // Used as an index to search array int subscript = -1; // To record position of search value while (index < size && subscript == -1) { if (array[index] == value) // If the value is found ___________________________________________________ ___________________________________________________ } if (subscript == -1) ___________________________________________________ return subscript; } Bonus…arrow_forward
- The return Or the throw function sometimes returns a random number and throws anither number as an exception. Write a code that will print out the number that the returnorthrow function gives back even if it throws an exception.arrow_forwardJava OOP Write a complete address book maintenance application. The user of the program has four options: Add a new person, delete a person, modify the data of a person, and search for a person by giving the name. Use the AddressBook class, either the original one from the chapter or the modified one from the previous exercises. You have to decide how to allow the user to enter the values for a new person, display person information, and so forth.arrow_forwardTask 7 Suppose you have a dictionary named my_dictionary containing the name of some groceries and their prices. my_dictionary={“Potato":12, "Onion":16, "Ginger":15, “Garlic":12, "Tomato":15} Now write a python program that takes the grocerie names as input from the users and shows the total price of the groceries. Some exceptions to handle: • Handle the exception when the given key is not present in the dictionary. Handle the exception when the summation variable is not initialized. (the variable to store the total price has been initialized at 0 or not) Example 1: Input: Potato,Onion, Ginger Output: 43 Example 2: Input: Potato, Onion, Ginger,Beef Output: Some groceries are not available in the dictionary.arrow_forward
- What is the value of the expression max().) O None It raises a ValueError exception. O It raises an IndexError exception.arrow_forwardTask 5: Unscramble You will write a program that takes in 2 files: a dictionary file and a file listing jumbled words. Your binary will be called unscramble and will be run using unscramble The file contains a bunch of scrambled words, one word per line. Your job is to print out these jumbles words, 1 word to a line. After each jumbled word, print a list of real dictionary words that could be formed by unscrambling the jumbled word. The dictionary words that you have to choose from are in the file. As an example, the starter package contains two sample input files and the result should look as follows. The order that you display the dictionary words on each line can be different; however, the order that you print out the jumbled words should be identical to the order in your input. The sample out below is skipping a few lines to save handout space (your output must not omit these lines). nwae: wean anew wane eslyep: sleepy rpeoims: semipro imposer promise ettniner: renitent.…arrow_forwardModify the program that you wrote for Exercise 5 so it handles the following exceptions: • It should handle any IOError exceptions that are raised when the file is opened, and data is read from it. • It should handle any ValueError exceptions that are raised when the items that are read from the file are converted to a number.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ 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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
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