Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Output issue
#1
So, I'm trying a simple "else if" and I am getting the correct output, however, it's also outputting "None".
What am I doing wrong here?

Thank you in advance.

def intro( name, age):
    if name != "" and age > 0:
        print (name, "was born in",2018 - age)
    elif (name == "" or age < 0):
        return ("invalid input")
    else:
        pass

print(intro("Brady",42))
Output:
Output:
Brady was born in 1976 None
Yoriz wrote Jul-29-2019, 10:43 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
The default return value from a function is None, when the if is true it does not return anything so it defaults to None
Instead of printing you can return the string and then it would print the returned string
print (name, "was born in",2018 - age)
change to
return f'{name}, was born in,{2018-age}'
twinpiques likes this post
Quote
#3
change the line 3 from print to return, you get the None because name != "" and age > 0 so the print in line 9 need something to prints and you in the line 3 don't return something
Yoriz and twinpiques like this post
Quote
#4
Thank you for the help - much appreciated.

So, I used "return" instead of "print".

def intro( name, age):
    if name != "" and age > 0:
        #print (name, "was born in",2018 - age)
        return f'{name}, was born in,{2018-age}'
    elif (name == "" or age < 0):
        return ("invalid input")
    else:
        pass

print(intro("Brady",42))

The "None" was resolved, however, I now get output with commas instead of spaces:

Output:
Brady, was born in,1976
I want to get:

Output:
Brady was born in 1976
Quote
#5
That is because you are returning a tuple instead of a string or you are using my f string from above which has commas in, just remove the commas.
return f'{name} was born in {2018-age}'
twinpiques likes this post
Quote
#6
(Jul-29-2019, 11:16 PM)Yoriz Wrote: That is because you are returning a tuple instead of a string or you are using my f string from above which has commas in, just remove the commas.
return f'{name} was born in {2018-age}'


Thank you for the help with the code and proper tag usage. :)
Much appreciated.
Quote
#7
Your welcome.
Please set the thread as solved if you are happy that your question has been resolved.
twinpiques likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  multithreading issue with output mr_byte31 4 307 Sep-11-2019, 12:04 PM
Last Post: stullis
  re.finditer issue, output is blank anna 1 371 Feb-07-2019, 10:41 AM
Last Post: stranac
  Paramiko output printing issue anna 3 7,376 Feb-06-2018, 08:34 AM
Last Post: anna
  Encoding issue for the console output ted_chou12 4 1,325 Sep-08-2017, 09:11 AM
Last Post: ted_chou12

Forum Jump:


Users browsing this thread: 1 Guest(s)