2

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__()
6
  • Try defining word_list before the function __secretword__ Commented Sep 10, 2017 at 19:09
  • 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. Commented Sep 10, 2017 at 19:12
  • 1
    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. Commented Sep 10, 2017 at 19:13
  • Whenever you cannot see an error on the line which is indicated in the error message, you should look at the previous line. Commented Sep 10, 2017 at 19:13
  • Also, on line 10, when you call the function, the return has nowhere to go. Try assigning it to a variable. Commented Sep 10, 2017 at 19:14

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

There are more small issues... Look around at it.
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.
Yes, I am debugging the whole thing right now. There are many small issues:) Thanks for the heads up ifconfig.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.