Help me with this invalid syntax

python

#1



https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2016/courseware/530b7f9a82784d0cb57de334828e3050/bfe9eb02884a4812883ff9e543887968/

debugfile('C:/Users/user/Documents/Python 6.00.1x Files/ps3_hangman.py', wdir='C:/Users/user/Documents/Python 6.00.1x Files')
File "c:/users/user/documents/python 6.00.1x files/ps3_hangman.py", line 136
elif guessInlowerCase not in secretWord:
^
SyntaxError: invalid syntax


I don't understand why? I thought everything is fine


# Hangman game
#

# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)

import random

WORDLIST_FILENAME = "words.txt"

def loadWords():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist

def chooseWord(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code
# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = loadWords()

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE...
    # split secretword in to a list, so it is iterable
    # check if ele in secretword found in letterGuessed, yes next ele, no return false
    
    secretWord_list=list(secretWord)
    for ele in secretWord_list:
        if ele not in lettersGuessed:
            return(False)
    return(True)
        



def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    # using list() to convert secretWord into a list
    # compare the ele in secretWord to lettersGuessed using for loop
    # if ele in secretWord inside lettersGuessed, change '_ _ _ _ _' to the letter guessed accordingly
    
    secretWord_list=list(secretWord)
    ans_list=['_ ']*len(secretWord)
    index=0
    for ele in secretWord_list:
        if ele in lettersGuessed:
            ans_list[index]=ele
        index+=1
    return(''.join(ans_list))
    
    #def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    # using list() to convert secretWord into a list
    # compare the ele in secretWord to lettersGuessed using for loop
    # if ele in secretWord inside lettersGuessed, change '_ _ _ _ _' to the letter guessed accordingly
    
    #secretWord_list=list(secretWord)
    #index=0
    #for ele in secretWord_list:
        #if ele in lettersGuessed:
            #ele=ele
        #else:
            #secretWord_list[index]='_'
       # index+=1
    
    #return(''.join(secretWord_list))        


def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
    import string
    lettersNotGuessed=string.ascii_lowercase
    lettersNotGuessed_list=list(lettersNotGuessed)
    for ele in lettersNotGuessed_list:
        if ele in lettersGuessed:
            lettersNotGuessed_list.remove(ele)
    return(''.join(lettersNotGuessed_list))
    
    
def reactionToGuess(secretWord,guessInlowerCase,lettersGuessed):
    '''
    secretWord: string, the secret word to guess.
    guessInlowerCase: string, a letter which the user entered to guess the word
    returns different strings if the user made a correct guess, wrong guess and repeated guess
    '''
    secretWord_list=list(secretWord)
    if guessInlowerCase in secretWord_list:
        return print('Good guess:'+ getGuessedWord(secretWord, lettersGuessed)
    elif guessInlowerCase not in secretWord_list:
        return print('Opps! That letter is not in my word: '+getGuessedWord(secretWord, lettersGuessed)
    elif guessInlowerCase in lettersGuessed_list:
        return print("Opps! You've already guess that letter: " +getGuessedWord(secretWord, lettersGuessed))
    
    
    
def hangman(secretWord):
    '''
    secretWord: 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 secretWord contains.

    * Ask the user to supply one guess (i.e. letter) per round.

    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computers word.

    * After each round, you should also display to the user the 
      partially guessed word so far, as well as letters that the 
      user has not yet guessed.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE...
    print('Welcome to the game, Hangman!')
    print('I am thinking of a word that is ' + len(secretWord)+ ' long')
    NumberofGusses=8
    lettersGuessed=[]
    
    while NumberofGusses>0:
        print('You have '+str(NumberofGusses)+' guesses left.')
        print(getAvailableLetters(lettersGuessed))
        guess=(input('Please guess a letter: '))
        guessInlowerCase=guess.lower()
        NumberofGusses-=1
        reationToGuess(secretWord,guessInlowerCase,lettersGuessed)
        lettersGuessed.extend(guessInlowerCase)
        if isWordGuessed==True:
            print('Congratulations, you won!')
            break
    print('Sorry, you ran out of guesses. The word was '+ secretWord)
    
        
        
        






# When you've completed your hangman function, uncomment these two lines
# and run this file to test! (hint: you might want to pick your own
# secretWord while you're testing)

# secretWord = chooseWord(wordlist).lower()
# hangman(secretWord)


#2

Please help me!
Waiting online for answer!


#3

Anyone there?
any kind soul please help


#4

There is the syntax error. Change that open paren to a space. Return values do not need to be in parens.


#5

Removed the paren for return, but the syntax error still persists


#6

Please post the traceback. Thanks.


#7

runfile('C:/Users/user/Documents/Python 6.00.1x Files/ps3_hangman.py', wdir='C:/Users/user/Documents/Python 6.00.1x Files')
File "C:/Users/user/Documents/Python 6.00.1x Files/ps3_hangman.py", line 136
elif guessInlowerCase not in secretWord_list:
^
SyntaxError: invalid syntax


#8

Missing closing parens on print statements, at or around line 136. (Multiple instances)


#9

Oh my God. Thank you so much.
I feel so dumb now. I am so so so careless.


#10

Go easy on yourself. Expect to make errors. We do not need to punish ourselves for them. Delight in the learning experience and forge ahead.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.