A function turns text into a whole number and doubles it, or gives -1 when it cannot.
def to_int(s):
try:
n = int(s)
except ValueError:
return -1
else:
return n * 2
print(to_int("12"), to_int("3.5"), to_int(" 7 "))
What does the program print?
Log in to join the discussion.
Loading…