C++ for Engineers and Scientists
C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
Bartleby Related Questions Icon

Related questions

Question
[画像:Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned if no exception is raised, just like for the open() function. If an exception is raised while trying to open the file, safeOpen() should return the value None. For example, assuming the file ghost.txt doesn't exist, the following is correct output: >>> # open() >>> print(open('ghost.txt')) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module>> print(open('ghost.txt')) FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt" >>> >>> # safeOpen() >>> inputFile=safeOpen('ghost.txt') >>>>> print(inputFile) None >>> Problem 2 Recall that when the built-in function float() is called it returns a floating point number constructed from a number or string. However, if the string doesn't represent a valid floating point value, an exception is raised. Write a function safeFloat() that takes one parameter, x-a number or string that needs to be converted to floating point number. When safeFloat() is used to convert a number or string, an equivalent floating point number is returned if no exception is raised, just like for the float() function. If an exception is raised while trying to convert a number or string, safeFloat() should return 0.0 floating point value. For example, the following is correct output: >>> # float() >>> float('abc') Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> float('abc') ValueError: could not convert string to float: 'abc' >>> # safeFloat() >>>f=safeFloat('abc') >>> print(f)]
expand button
Transcribed Image Text:Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned if no exception is raised, just like for the open() function. If an exception is raised while trying to open the file, safeOpen() should return the value None. For example, assuming the file ghost.txt doesn't exist, the following is correct output: >>> # open() >>> print(open('ghost.txt')) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module>> print(open('ghost.txt')) FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt" >>> >>> # safeOpen() >>> inputFile=safeOpen('ghost.txt') >>>>> print(inputFile) None >>> Problem 2 Recall that when the built-in function float() is called it returns a floating point number constructed from a number or string. However, if the string doesn't represent a valid floating point value, an exception is raised. Write a function safeFloat() that takes one parameter, x-a number or string that needs to be converted to floating point number. When safeFloat() is used to convert a number or string, an equivalent floating point number is returned if no exception is raised, just like for the float() function. If an exception is raised while trying to convert a number or string, safeFloat() should return 0.0 floating point value. For example, the following is correct output: >>> # float() >>> float('abc') Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> float('abc') ValueError: could not convert string to float: 'abc' >>> # safeFloat() >>>f=safeFloat('abc') >>> print(f)
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning