简体   繁体   中英

Python - check output from routine

I have no python experience what so ever, in fact, my only programming experience is on COBOL.

Just for fun (and at my own pace) I am taking "Introduction to Computer Science and Programming" course on MIT OpenCourseWare. On a problem set we have to create a Hangman wordgame using python.

On hangman game, to check if the letter user guesses exists in the Game Word I wrote the following code:

def check_letter_word(letter, game_word, chosen_word):  
    letter_found = 'n'  
    for i in xrange(len(chosen_word)):  
        if letter == chosen_word[i]:  
            letter_found = 's'  
            game_word[i] = letter  
            print ' DEBUG 1: wordout[i]:', game_word[i]  
            print ' DEBUG 2: letter:', letter  
    return game_word  

if check_letter_word(guessed_letter, game_word, chosen_word) == chosen_word:
     winner_found = 's'

Now, I need to check two things: If letter_found is equal to 'n'. If it is the player has one less attempt. If game_word is equal to the word defined in the beggining. If it is the player wins.

How can I do this? Can I use "check_letter_word" to return both variables at once and evaluate them?

Thanks in advance,

You can just return a tuple from check_letter_word : return game_word, letter_found . You can then use the automatic unpacking to assign the returned values to two different variables: game_word, letter_found = check_letter_word(...) and evaluate them the way you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM