Python Forum

Full Version: I get "None" at the end of my printed result.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I get "None" at the bottom of my printed result and I don't know where this "None" is coming from.

All works fine. Just really don't need this "None" message in the end. Your help is much appreciated.

Here's my code:

def str_analysis(your_input):
        if your_input.isdigit():
            if int(your_input) > 99:
                print("Big number")
            elif int(your_input) <= 99:
                print("Small number")
            else:
                pass
        elif your_input.isalpha():
            print("Your message is all Alpha characters")
        else:
            print("Your message is neither all Alpha nor all Digits")
        return


check = str_analysis(input("Enter your message: "))   

print(check)
You are assigning "check" the outcome of your function str_analysis()
and the default value of return is None.
So "print(check)" prints "None".

Better pythonic way:
DonĀ“t use print() inside a function that is doing something.
Always return the value of the outcome of this "doing".

def str_analysis(your_input):
        if your_input.isdigit():
            if int(your_input) > 99:
                return "Big number"
            elif int(your_input) <= 99:
                return "Small number"
            else:
                pass
        elif your_input.isalpha():
            return "Your message is all Alpha characters"
        else:
            return "Your message is neither all Alpha nor all Digits"
str_analysys has empty return statement at the end so it will return None. Even without explicit return any function will return None if you don't specify return value. You assign that to check
when you run your function it print respective message and return None. on line 18 you print that None
This makes sense. Thanks a lot to both of you!