I'm still learning the ropes with my programming and have run into an issue that I can't seem to solve after searching for the answers
I have a while loop that is checking if a dictionary is not empty, or if the user entered a specific character (escape character).
The program works as expected if the user enters the escape character on the first prompt but seems to not function correctly if they first enter something else and then try it.
Can anyone help?
hand = {'p':1, 'y':1, 't':1, 'h':1, 'o':1, 'n':1}
def play_hand(hand):
print hand
word = raw_input('Enter word, or a "." to indicate that you are finished: ')
while any(hand) is True or word not in '.':
if word == '.':
break
elif (word == 'toy' or word == 'python') and (word != '.'):
print '"'+ word +'"', 'earned', 'points.'
play_hand(hand)
else:
print 'Invalid word, Please try again.'
return word, play_hand(hand)
else:
print 'Ending'
return
So as suggested I restructured the code and removed the recursion from the inner IF ELSE statements. It did the trick! Thanks for the help.
Here's the sample from above done in the same fashion. You can now enter the valid word and also exit using the escape character.