Why wont this ever print?


#1



my code works fine but it will not print the responses or number that it should?
thanks


x = int(raw_input("enter number: "))

def factorial(x):
    num = 1
    if x > 0:
        for i in range(1, x + 1):
            num =  num * i
        return num
        print num
        
    elif x == 0:
        return 1
        print 1
    
    else:
        print "please enter positive number"
    
.


#2

Your code asks for input and waits for that input.
After that, the input is converted to int.
Finally, a function is created.

Nothing else happens, so aside from asking for the input, nothing will be printed, there's no instruction that amounts to that.

So whichever part you meant should print something, consider how program execution is supposed to reach that location in the code


#3

a function doesn't execute unless called


#4

That definitely makes sense. However, if I put a factorial( ) at the bottom everything still wont print.

y = int(raw_input("enter number: "))

def factorial(x):
    num = 1
    if x > 0:
        for i in range(1, x + 1):
            num =  num * i
        return num
        print num
        
    elif x == 0:
        return 1
        print 1
    
    else:
        print "please enter positive number"

factorial(y)

EDIT: It works if i put print factorial(y)...But i have print inside the function, so why do i need to put print outside when calling the function?


#5

your function returns, it doesn't print anything. You can print what the function returns by adding a print statement in front of the function call.

A function ends the moment a return keyword is reached, so the print statements inside your function are never executed


#6

thank you very much sir. it all makes sense now.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.