0

So i am practicing with coding in python. I always use a return statement if i need to return a number so i dont get none. But now when i just want to print out a comment it returns none. Even when i try to use a return statement, it returns a none.

def name_tag():
    name = input("Hello, whats your name? : ")
    print(f'Hi {name}, would you like to play a game?')
    print(y_n())

def y_n():
    yes_no = input("Y/N: ")

    if yes_no.lower() == 'y':
        print('Cool, than lets play a game')

    elif yes_no.lower() == 'n':
        print('To bad!')

    else:
        print('Please anwser with [y] or [n]')
        print(y_n())

This is the output

Hello, whats your name? : jop
Hi jop, would you like to play a game?
Y/N: k
Please anwser with [y] or [n]
Y/N: y
Cool, than lets play a game
None
None
None
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jop
  • 15
  • 4
  • functions always return even without a return statement. From [the docs](https://docs.python.org/3/tutorial/controlflow.html#defining-functions) : `Falling off the end of a function also returns None.` – wwii Sep 25 '19 at 18:25
  • Related: [return, return None, and no return at all?](https://stackoverflow.com/questions/15300550/return-return-none-and-no-return-at-all) – wwii Sep 25 '19 at 18:28
  • Possible duplicate of [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – wwii Sep 25 '19 at 18:28

3 Answers3

2
print(y_n())

prints the return value of the y_n function. That value is implicitly None as the function does not return anything.

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • is there a better way to return the question Y/N if someone does not give a y or n answer? – Jop Sep 25 '19 at 18:32
1

You are printing the results of y_n(), which is None try this:

def name_tag():
    name = input("Hello, whats your name? : ")
    print(f'Hi {name}, would you like to play a game?')
    y_n()

def y_n():
    yes_no = input("Y/N: ")

    if yes_no.lower() == 'y':
        print('Cool, than lets play a game')

    elif yes_no.lower() == 'n':
        print('To bad!')

    else:
        print('Please anwser with [y] or [n]')
        y_n()

If you want to use print(y_n()) you could change it so you are returning the strings from y_n():

def name_tag():
    name = input("Hello, whats your name? : ")
    print(f'Hi {name}, would you like to play a game?')
    print(y_n())

def y_n():
    yes_no = input("Y/N: ")

    if yes_no.lower() == 'y':
        return 'Cool, than lets play a game'

    elif yes_no.lower() == 'n':
        return 'To bad!'

    else:
        print('Please anwser with [y] or [n]')
        return y_n()
Mason Caiby
  • 1,846
  • 6
  • 17
  • I tried both, but it still returns one none. Or is there a while loop that keeps asking you Y/N if you do not give a y or n – Jop Sep 25 '19 at 18:29
  • It doesn't print `None` for me. are you calling `name_tag()` and how are you running it? – Mason Caiby Sep 25 '19 at 18:34
  • I copy and pasted it in PyCharm and than runned with te run button. It now returns 1 none and not 3 – Jop Sep 25 '19 at 18:56
  • You're not running just my code in PyCharm and getting output, because my code doesn't call any of the functions. What are you adding to the file in pycharm? if you have `print(name_tag)()` you will get a `None` printed to your terminal. You should just have `name_tag()` at the end of your file. – Mason Caiby Sep 25 '19 at 18:58
  • Thank you! that print(name_tag()) was the problem! – Jop Sep 25 '19 at 19:38
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – Mason Caiby Sep 25 '19 at 20:23
0

Your code outputs None because you call print(y_n()) which prints the return value of the y_n() function which is None. What you should do is call the function without using print() because the output value is already being printed inside the function.