C++ for Engineers and Scientists
C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
expand_more
expand_more
format_list_bulleted
Bartleby Related Questions Icon
Related questions
Question
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 MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
Step by stepSolved in 2 steps
Knowledge Booster
Background pattern image
Recommended textbooks for you
- Text book imageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrText book imageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTText book imageProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Text book imageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningText book imageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Text book imageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning
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