Python Forum
I get "None" at the end of my printed result.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I get "None" at the end of my printed result.
#1
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)
Reply
#2
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"
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
This makes sense. Thanks a lot to both of you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 418 Feb-02-2024, 06:06 PM
Last Post: tester_V
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 852 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  Reverse printed words hindubiceps 7 2,917 Apr-21-2020, 05:19 PM
Last Post: deanhystad
  bytes not being printed as expected Skaperen 2 2,322 Aug-27-2019, 05:33 AM
Last Post: Skaperen
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 2,622 Jul-12-2019, 06:01 AM
Last Post: Gribouillis
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 2,315 Feb-05-2019, 12:52 PM
Last Post: perfringo
  Why A will be printed twice in the picture Shen 3 4,031 Jul-25-2018, 01:16 PM
Last Post: stranac
  0 error but Not printed Piqurs 5 3,908 Jul-14-2018, 04:43 PM
Last Post: buran
  [Variable as a String]>EscaSpecial characters printed CSA75 4 4,242 Mar-28-2017, 11:59 AM
Last Post: snippsat
  Best software for creating printed reports? birdieman 5 4,358 Feb-02-2017, 02:39 AM
Last Post: birdieman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020