Posts: 2
Threads: 1
Joined: Jul 2022
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
Posts: 12,023
Threads: 484
Joined: Sep 2016
change line 28 to return word
Posts: 77
Threads: 31
Joined: Dec 2020
Jul-16-2022, 03:10 AM
(This post was last modified: Jul-16-2022, 03:10 AM by Oshadha.)
(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
Posts: 1,838
Threads: 2
Joined: Apr 2017
Jul-16-2022, 09:49 AM
(This post was last modified: Jul-16-2022, 09:49 AM by ndc85430.)
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.
Posts: 2
Threads: 1
Joined: Jul 2022
Thank you for the help! I changed to return word and worked fine.
Posts: 6,779
Threads: 20
Joined: Feb 2020
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']))
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
|