0

I'm quite new at Python, so forgive me if my question is silly, however, I cannot understand what's the difference between two functions below and why the second one works when exception is used and the fist one doesn't. I tried swapping 'result' with 'return' but it doesn't help. I was thinking that my condition for missing argument is incorrect but even if I remove it and leave only return 'month missing', the exception is still not printed.

Thanks for your help.

def after_n_months(year, month):
    try:
        result year + (month // 12) 
    except TypeError:
        return 'month missing' if month is None else 'year missing' 
print(after_n_months( 2000, ))


def divide(x, y):
    try:
        result = x / y
    except ZeroDivisionError:
        return "division by zero!"
print(divide(2,0))
3
  • It should bereturn year + (month // 12) Commented Jun 10, 2020 at 21:42
  • Can you post the actual error? You should get a syntax error because of result. After that, you should get a TypeError when calling the first function. Its because 2 positional arguments are required and this happens before the function is called and so the function's exception handler is not envolved. Commented Jun 10, 2020 at 21:48
  • 1
    One positional argument is missing, to test the exception you should run it with: print(after_n_months( 2000, None)) Commented Jun 10, 2020 at 21:48

1 Answer 1

1

It's because TypeError exception is catch before you enter in the function.

Try this instead

def after_n_months(year, month):
    return year + (month // 12) 

try : 
    print(after_n_months(2000))
except:
    print("error")

EDIT : To test your function you can also put into None

def after_n_months(year, month):
    try:
        return year + (month // 12) 
    except TypeError:
        return 'month missing' if month is None else 'year missing' 
    print(after_n_months( 2000 ))

print(after_n_months(2000, None))
print(after_n_months(None, 12))

EDIT2: You can also put your function inputs in None, like this and remove the expections, because it's not needed.

def after_n_months(year = None, month = None):
        if year is None:
            print("Year missing")
            return None
        if month is None:
            print("Month is missing")
            return None
        return year + (month // 12) 

after_n_months(5,12)
after_n_months(year=5)
after_n_months(month=12)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi all, thanks for the suggestions. They are helpful, however, what I'm trying to achieve is to avoid TypeError: after_n_months() missing 1 required positional argument: 'month' in case one of the arguments is actually missing. Your suggestion does work if None is typed as one of the arguments, however, what if one argument is actually missing (only year or only month is given). Whenever I try this, TypeError is always returned..
If you want to see if one element is missing you can do def after_n_months(year=None, month=None) And add a condition if month is None: if year is None:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.