I have a problem in function anyone help me in this

Why I am unable to print the result if i am putting number%3==0 in VS CODE editor and if I am using below code I am getting my output but i am using above condition in my program i dont get any result whereas if I am using same code in Codecademy Editor, i am getting error anyone explain me Why I am getting this?

def cube(number):

    x = number**3

    return x

def by_three(number):

    if number/3:

        return print(cube(number))

    else:

        return False

number = int(input(" "))

by_three(number)

Hi there.

It’s because in your code at the moment, your if condition will always return as True.

You’ve written number/3 as your condition, which is a “truthy” value. As a result, you’re always executing the if branch and writing to the console the value of cube(number).

With number % 3 ==0, you have a condition which is not always true. If you enter a number which is a multiple of 3 - which will have no remainder, and so satisfy the condition - then you’ll see output to the console. Otherwise, you’ll execute the else branch which returns a value to the caller but doesn’t send anything to the console.

3 Likes

thanks but why i am unable to print the above code in vscode.?

Your program ought to work the same way in either the terminal used by Visual Studio Code, and the Codecademy learning environment (LE).

From what I can gather, in VSCode you’re changing the condition for your if branch to number % 3 == 0. As I said before, this will only evaluate to True - and your if branch will only execute if this is the case - if the number you input is a multiple of 3.

Your else branch produces no output to the console, so if you entered 5 into your program for example you would get no visible output. Your program is still working, and running as instructed, but you’ve not told it to print() anything to the console in that branch.

1 Like