-1

I'm super new to programming, like it's been less than a week and I've been tasked to write this simple program that uses functions that I wrote. From what I know, I don't understand why the code isn't working. this is my code:

def filter_teens(a=13, b=13, c=13):

    def fix_age(age):
        if (int(age) < 15 and int(age) >= 13) or (int(age) > 16 and int(age) <= 19) :
            new_num = int(age) - int(age)
            return new_num


    if a != 15 or a != 16:
        result_a =  fix_age(a)
    else:
        result_a = int(a)

    if b != 15 or b != 16:
        result_b = fix_age(b)
    else:
        result_b = int(b)

    if c != 15 or c != 16:
        result_c = fix_age(c)
    else:
        result_c = int(c)

    final_result = int(result_a) + int(result_b) + (result_c)
    return final_result


sum = int(filter_teens(15, 13, 16)) ### I receive the error here
print(sum)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    Please post the full error traceback when asking about errors in your code – Tomerikoo Feb 08 '21 at 17:30
  • [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](/q/25385173/843953) Learning [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) is an extremely valuable skill for a programmer. Please go through the linked page and apply the techniques described there to your problem to narrow down the cause of the error. Then ask a specific question. _"Why is my code doing this"_ is [too broad for Stack Overflow.](//meta.stackoverflow.com/a/253788/843953) – Pranav Hosangadi Feb 08 '21 at 17:30
  • Please also take the [tour], read [ask] and [what's on-topic](/help/on-topic). Welcome to Stack Overflow! – Pranav Hosangadi Feb 08 '21 at 17:31
  • 3
    Try to think what happens in `fix_age` if the `age` doesn't pass the criteria – Tomerikoo Feb 08 '21 at 17:31
  • 1
    `if a != 15 or a != 16:` will always be true. – interjay Feb 08 '21 at 17:37

2 Answers2

0

Your "a" value of 15 doesn't satisfy the conditional in your fix_age function that returns a value. By the logic, a value is only returned if the value passed to fix_age is 13, 14, 17, 18, or 19. Since "a" is 15 the compiler is returning NoneType instead, which can't be converted to an integer. You'll need to have a return value outside the conditional to accommodate this, or update the conditional.

0

The return statement in your fix_age function is only called inside the if statement. As far as Python is concerned you can always call this function in such a way that the if statement is not satisfied. In that case the function will not return a number but a 'NoneType'. Since every input argument in filter_teens is used as input for fix_age, they might all potentially be 'NoneType'. Calling int() on such a 'NoneType' is not possible, hence the error.

I do not know what your intended goal is, but a fix might be to return the number 0 from the fix_age function if the if statement is not True. This would look like this:

def fix_age(age):
        if (int(age) < 15 and int(age) >= 13) or (int(age) > 16 and int(age) <= 19) :
            new_num = int(age) - int(age)
            return new_num
        else:
            return 0

One extra tip: Python is very good in inferring types, so you can probably lose all the int() calls around the numbers in your code and it would still work (and be more easy to read).

Erik
  • 722
  • 4
  • 11