Raising an Exception You are to write a function string2int that attempts to con
ID: 3561091 • Letter: R
Question
Raising an Exception You are to write a function string2int that attempts to convert a string to an integer. If the string does represent a positive integer then that integer should be returned. If the string does not represent a positive integer then you should raise a syntax exception (raise SyntaxError (not an integer )). You may choose to use the (already defined) function all_digits that takes a string and returns True if all the characters of the string are digits and False otherwise. You may assume you don't need to deal with strings representing negative integers (such as -42)Explanation / Answer
def string2int( value ):
if all_digits(value):
return value
else:
raise SyntaxError('not an integer')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.