Python Forum
MIt 6.0001 open courseware pset 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MIt 6.0001 open courseware pset 2
#1
hello
i am very new to coding and have decided to start out trying to learn python (I'm using python 3). so far i've been reading 'learn python the hard way' (and doing the 'finger exercises) as well as running through the MIT opencourseware for class 6.0001 (its intro to computer science using python). So far when trying to work the psets for this class I've been ok since I haven't needed much help but pset2(hangman game) has been a bit of a brick wall for me.

Firstly I'd like to see if anyone on here has any guidance to offer as to where I could get help with these psets (i've searched but haven't found any pset answer sheets etc)

Secondly, i feel like i've gotten pretty close to getting a working version of the game but with one major issue: if the user correctly guesses the first letter in the secret word, the game acts as though the entire word has been solved.
i have created a function:
 
 for char in secret_word[:]: #i used [:] here to try to specify all chars in secret_word (thinking this might be key to solving my issue here?
     if char not in letters_guessed:
         return False
     else: 
         return True
which is called in the general game function like this:
def hangman(secret_word):
    '''
    secret_word: string, the secret word to guess.
    
    Starts up an interactive game of Hangman.
    
    * At the start of the game, let the user know how many 
      letters the secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!
    
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
    
    Follows the other limitations detailed in the problem write-up.
    '''
    

    letters_guessed = []
    turns = 5
    
    print("Welcome to hangman!")
    print("The secret word constains", len(secret_word), "letters.")
    print("-----------------------")
    
    while True:
        available_letters = get_available_letters(letters_guessed)
        letters = string.ascii_lowercase
        print("You have", turns + 1, "turns left.")
        print("Available letters: ", available_letters)
        letter = input("Please guess a letter: ")
        
    
        if letter in secret_word:
            print("Good guess!")
        else:
            print("Letter is not in secret word")
            
        if letter in letters_guessed:           
            print("You've already guessed that one!")
             
        for char in letter:
            if char not in letters:
                print("That wasn't a lowercase letter!")
                print("You have lost a turn")
                
        letters_guessed.append(letter)
        
        if is_word_guessed(secret_word, letters_guessed) is True:
            print("You've guessed the word!")
            print(get_guessed_word(secret_word, letters_guessed))
            return False
            break
        else:
            print("-----------------------")
            print(get_guessed_word(secret_word, letters_guessed))
            
        turns = turns - 1
        if turns < 0:
            print("You are out of turns!")
            print("The secret word was", secret_word)
            break
Any help with this will be greatly appreciated.
Reply
#2
[:] just makes a copy of whatever it slices, it otherwise doesn't change how that thing is iterated over.

For your problem note that you only want to return True if none of the characters return False. So you put the return True after and outside the loop:

for char in secret_word[:]: 
    if char not in letters_guessed:
        return False
return True
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020