无效的语法指向python中的“ return”

Hun*_*inh 0 python syntax python-3.x

我只是在探索的广泛语言python。我在很大程度上依靠内置的错误检测和谷歌搜索来调试程序。这次我没有运气。感谢您的时间!

我正进入(状态

“第5行的语法无效”

这是我的简单的python代码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__()
Run Code Online (Sandbox Code Playgroud)

Mat*_*ipp 8

您的问题是,前一行return缺少其右方括号]。因此,Python认为进入第5行时您仍在方括号内,而放在return方括号内是语法错误。

这是一个相当普遍的问题-如果您在看起来不错的行上遇到语法错误,通常值得去看一下前一行,看看您的行是否被认为是其中的一部分。

  • 是的,但是这就是为什么代码给出“第5行语法错误”(带有“ return”)的原因,这就是问题所在。第10行在交互使用中会很好(并且很常见),并且如果构成任何逻辑错误且可以轻松调试,则不是语法错误。 (3认同)