im trying to save an input on a list and when the function is running its checking if the letter is on the list, if it is a new guess its return True. but i dont sucess to save the guesses in the variable. thanks for help ! (im even ask from the function to print the list to show if its save the guesses but its not)
letter_guessed = input("please guess a letter").lower()
old_letters_guessed = [ ]
def check_valid_input(letter_guessed, old_letters_guessed):
"""
this function will check if the the guess is ok
:param letter_guesses: the user's guess
:type letter_guesses: str
:return: True if the guess is complies with the rules and False if not
:rtype: bool
"""
import string
if letter_guessed.isalpha() and len(letter_guessed) == 1 and letter_guessed not in old_letters_guessed :
return(True)
else:
return(False)
old_letters_guessed.append(letter_guessed)
print(old_letters_guessed)
check_valid_input(letter_guessed, old_letters_guessed)
CodePudding user response:
You have return statement in both clauses of the if/else - the function returns before old_letters_guessed.append(...
is executed. You need to move the append statement before the return statements.
Here is one way to do it.
is_valid = (letter_guessed.isalpha()) and (len(letter_guessed) == 1) and (letter_guessed not in old_letters_guessed)
if is_valid: old_letters_guessed.append(letter_guessed)
return is_valid
CodePudding user response:
You are returning the function in the if else statement itself. Any code after the return won't work.
letter_guessed = input("please guess a letter").lower()
old_letters_guessed = [ ]
def check_valid_input(letter_guessed, old_letters_guessed):
"""
this function will check if the the guess is ok
:param letter_guesses: the user's guess
:type letter_guesses: str
:return: True if the guess is complies with the rules and False if not
:rtype: bool
"""
old_letters_guessed = []
import string
old_letters_guessed.append(letter_guessed)
print(old_letters_guessed)
if letter_guessed.isalpha() and len(letter_guessed) == 1 and letter_guessed not in old_letters_guessed :
return(True)
else:
return(False)
check_valid_input(letter_guessed, old_letters_guessed)
This should work.