Python Forum
Why my function is returning None?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why my function is returning None?
#1
Hello,

Someone can explain why my function is returning None, please? I know that, if I add a "return(word)" in the last line of the "else" block, the output doesn't return "None", but neither the letters guessed so far (it's just output the underscores created by the while). Thanks for the help.

def get_guessed_word(secret_word, letters_guessed):
   '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    word = ""
    i = 0
    while i < len(secret_word):
        word = word + "_ "
        i += 1
    
    j = 0
    for char1 in secret_word:
        j+=1
        for char2 in letters_guessed:
            if char1 == char2 and j != 1:
                word = word[0:2*j-2] + char2 + word[2*j-1:2*len(secret_word)]
                
            elif char1 == char2 and j == 1: 
                word = char2 + word[1:2*len(secret_word)+1] 
                
            else:
                word = word
                
            
    return print(word)

print(get_guessed_word('house', ['t', 'o', 'k', 'e']))
Output:
_ o _ _ e None
Reply
#2
change line 28 to return word
Reply
#3
(Jul-16-2022, 12:57 AM)PauloDAS Wrote: Hello,

Someone can explain why my function is returning None, please? I know that, if I add a "return(word)" in the last line of the "else" block, the output doesn't return "None", but neither the letters guessed so far (it's just output the underscores created by the while). Thanks for the help.

def get_guessed_word(secret_word, letters_guessed):
   '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    word = ""
    i = 0
    while i < len(secret_word):
        word = word + "_ "
        i += 1
    
    j = 0
    for char1 in secret_word:
        j+=1
        for char2 in letters_guessed:
            if char1 == char2 and j != 1:
                word = word[0:2*j-2] + char2 + word[2*j-1:2*len(secret_word)]
                
            elif char1 == char2 and j == 1: 
                word = char2 + word[1:2*len(secret_word)+1] 
                
            else:
                word = word
                
            
    return print(word)

print(get_guessed_word('house', ['t', 'o', 'k', 'e']))
Output:
_ o _ _ e None
Full Explanation:

You are using print() twice in both line 28 and 30
It is important that when returning a value, always return it as a variable, and not a line of code;
def func(x):
    return print(x) # Not like this

print(func("Hello")) # when you do this, you are doing something similar to the following;
print(print("hello"))
Basically printing twice.

# Better
def func_better(x):
    return x # Return as a value

print(func_better("Hello")) # This is the correct method
Reply
#4
Oshadha Wrote: It is important that when returning a value, always return it as a variable, and not a line of code

This really isn't a correct thing to say at all. First, returning the value of any kind of expression is perfectly fine. An expression is something that can be evaluated to produce a value: examples are: a function call as in the OP's example, a literal (like 123 or "foo", or a variable.

The problem is that the print function returns None - since its job is to print to the screen, it doesn't make sense for it to return any meaningful value.
buran likes this post
Reply
#5
Thank you for the help! I changed to return word and worked fine.
Reply
#6
To demonstrate ndc's point about it being ok to return the result of an expression without assigning it to a variable. This returns the result of str.join() and it works fine.
def get_guessed_word(secret_word, letters_guessed):
    word = []
    for letter in secret_word:
        word.append(letter if letter in letters_guessed else "_")
    return " ".join(word)
 
print(get_guessed_word('house', ['t', 'o', 'k', 'e']))
Or with no local variables at all.
def get_guessed_word(secret_word, letters_guessed):
    return " ".join([x if x in letters_guessed else "_" for x in secret_word])
 
print(get_guessed_word('house', ['t', 'o', 'k', 'e']))
Reply
#7
the original code, before you fixed it, was returning whatever print() returned to it. if print() returns 42 then your function will return 42. since your function returned None, what do you think print() returned?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pausing and returning to function? wallgraffiti 1 2,123 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Why is the function returning None for a * b instead of number? omm 10 4,202 Nov-05-2020, 01:17 PM
Last Post: omm
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,449 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  Returning Value from Function with Trackbars vicpylon 3 2,024 May-24-2020, 11:28 PM
Last Post: bowlofred
  Nested Recursive Function not Returning Etotheitau 2 2,217 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Function not returning correct value ActualNoob 3 2,655 Jan-11-2019, 12:35 AM
Last Post: stullis
  Function not returning expected value Euqinu 4 3,431 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  Recursive function not returning expected output...(Python speech recog module) bigmit37 4 5,814 Jan-10-2017, 02:13 PM
Last Post: bigmit37
  function returning None tkj80 3 5,216 Oct-06-2016, 02:08 AM
Last Post: tkj80

Forum Jump:

User Panel Messages

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