Hi, I'm working on pset 5 doing the ghost game. I do not understand why the pg does not halt when the player fails (second if and first elif statement below). Instead it keeps asking for a new letter. What am I doing wrong? main loop for ghost game def play_ghost(player,word): go = 'y' while go == 'y': letter = raw_input('Please enter a letter >> ') if letter not in string.ascii_letters: print 'Not a valid letter.' else: print player+' says letter '+letter word = word+letter # if fgt is word of more than 3 letters, failure if len(word) > 3 and word in wordlist: print '%s loses because %s is a word!\n'%(player,word) go = 'no' # if fgt does not start a word failure elif not prefix(word,wordlist): print '%s loses because no word starts with %s!\n'%(player,word) go = 'no' # else switch player else: new_player = switch[player] print '\nCurrent word fragment is %s'%(word) print "%s's turn"%(new_player) play_ghost(new_player,word) print "Good bye!"
please use a code pasting site to post your code dpaste.com pastebin.com codepad.org .....
Ok so here is the link http://dpaste.com/hold/750836/ and my question again: why does the pg not halt when the player fails (second if and first elif statement below). Instead it keeps asking for a new letter.
It is because this is a recursive function. Since play_ghost() calls itself in the second else statement, even if that call ends in a failure, the while loop in the current play_ghost() call won't end because your value of go is still 'y'. My suggestion: remove the while loop because your recursive function already does the looping.
Yes of course. Thanks a lot. Here is the revised code http://dpaste.com/751020/
maybe the function should return for every conditional that results in a player loosing. please post your prefix function, I'd like to see it.
Does it work now?
Join our real-time social learning platform and learn together with your friends!