double_or_nothing
0
Write your function and test it.
Examples:
double_or_nothing(5) # returns 10 double_or_nothing(-3) # returns 0 double_or_nothing(0) # returns 0
Looking ahead
try
except
try: num = int(input("Enter a number: ")) print(10 / num) except: print("Something went wrong!")
try: num = int(input("Enter a number: ")) print(10 / num) except ValueError: print("That wasn’t a number!") except ZeroDivisionError: print("You can’t divide by zero!")
def safe_divide(a, b): try: return a / b except ZeroDivisionError: return "Error: Division by zero" print(safe_divide(10, 2)) # 5.0 print(safe_divide(10, 0)) # Error: Division by zero
except:
Write a function to_int(s) that:
to_int(s)
"Error: not a number"