Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid syntax points to "return" in python [duplicate]

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__()
like image 682
Hung Trinh Avatar asked Jul 29 '26 03:07

Hung Trinh


1 Answers

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.

like image 151
Matthias Fripp Avatar answered Jul 30 '26 18:07

Matthias Fripp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!