Asked by Malcom
URGENT: Can anyone please show me how to write this code in python??
Prompt:
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)
0.0
>>>
Prompt:
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)
0.0
>>>
Answers
There are no human answers yet.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.