Search code examples
pythonfunctionreturn

Function returns nothing rather than the expected value "None"


I have no return put into my function, so I thought it should return "None". Instead, it simply does not return anything. Can someone tell me why? All help appreciated!

def posdivisor(n):
    for i in range(1,n+1):
        if n % i == 0:
            print(i)

someValue = eval(input("Enter an integer: "))

posdivisor(someValue)

THE SHELL REPORTS:

Enter an integer: 49
1
7
49

Solution

  • Because your code just print data, the function return a None, and you ignore it, try to print out will see None:

    def posdivisor(n):
        for i in range(1,n+1):
            if n % i == 0:
                print(i)
    
    someValue = eval(input("Enter an integer: "))
    
    result = posdivisor(someValue)
    print result
    

    Besides, you don't need eval() here, just input() will be ok if you can insure that input is always number:

    def posdivisor(n):
        for i in range(1,n+1):
            if n % i == 0:
                print(i)
    
    someValue = input("Enter an integer: ")
    
    result = posdivisor(someValue)
    print result