invalid syntax points to "return" in python

18,173

Your problem is that the line before return is missing its closing square bracket ]. So Python thinks you're still inside the brackets when you get to line 5, and placing return inside brackets is a syntax error.

This is a fairly common problem - if you get a syntax error on a line that looks fine, it's often worthwhile to look at the previous line, and see if your line might be being considered part of that.

Share:
18,173
Hung Trinh
Author by

Hung Trinh

I am a high school student from Norway learning how to code. I love code:D

Updated on July 14, 2022

Comments

  • Hung Trinh
    Hung Trinh almost 2 years

    I am just exploring the vast language of python. I'm heavily relying on the inbuilt error-decetion and googling to debug my programs. I had no luck this time around. Thank you for your time!

    I am getting

    "invalid syntax at line 5"

    Here is my python-code of a simple HANGMAN-game:

    import random
    
    def __secretword__():
        secret_word = word_list[random.randomint(len(word_list))
        return secret_word #THIS IS LINE 5
    
    def __ask__():
        ans = input("Would you like to play more? (yes/no): ")
        if ans == "yes"
            __secretword__()
        else:
            print(":(")
    
    
    __secretword__()
    
    word_list = ["nei", "brun", "ja", "smile", "glad", "gal"]
    secret_word = secret_word
    sw_list = list(secret_word)
    guess_lines = ["_"] * len(secret_word)
    mistakes = []
    lives = 2 * len(secret_word)
    
    print(guess_lines)
    
    user_guess = input("Guess a letter: ")
    
    while(lives != 0)   
        if user_guess in sw_list:
            i = sw_list.index(user_guess)
            del guess_lines[i]
            guess_lines.insert(i, user_guess)
            print(guess_lines)
            print("Lives: ", lives)
            print("Mistakes: ", mistakes)
            if "_" not in guess_lines:
                break
        else:
            mistakes.append(user_guess)
            print(guess_lines)
            lives = lives - 1
            print("Lives: ", lives)
            print(mistakes)
    
    __ask__()
    
    • Cuber
      Cuber over 6 years
      Try defining word_list before the function __secretword__
    • DYZ
      DYZ over 6 years
      You are missing a closing bracket in word_list[random.randomint(len(word_list)). Incidentally, if the error is on line 5, do not post the remaining irrelevant lines.
    • Charles Duffy
      Charles Duffy over 6 years
      BTW, we ask that questions around code have a minimal reproducible example -- the smallest possible self-contained code that produces the same problem when someone else invokes it. This is much, much larger than what you need to produce the question you're asking about.
    • Code-Apprentice
      Code-Apprentice over 6 years
      Whenever you cannot see an error on the line which is indicated in the error message, you should look at the previous line.
    • ifconfig
      ifconfig over 6 years
      Also, on line 10, when you call the function, the return has nowhere to go. Try assigning it to a variable.
    • Hung Trinh
      Hung Trinh over 6 years
      DYZ and Cuber - Thanks! I didn´t see that
  • ifconfig
    ifconfig over 6 years
    There are more small issues... Look around at it.
  • Matthias Fripp
    Matthias Fripp over 6 years
    Yes, but this is why the code is giving "syntax error at line 5" (with a return), which is what the question asked about. Line 10 would be fine (and common) in interactive use, and if anything it constitutes a logical error which could be debugged easily, not a syntax error.
  • Hung Trinh
    Hung Trinh over 6 years
    Yes, I am debugging the whole thing right now. There are many small issues:) Thanks for the heads up ifconfig.