NamerError:名称未定义python

xiaoxingxing python 204

原文标题NamerError: name was not defined python

这是我的程序,用户将输入一个流派并输出电影/电影,如果他们输入“停止”,那么程序将打印一些东西,但是当我尝试运行它时,它说“user_genre”没有定义,即使它是。

Anabelle=["Horror", "Triller"]
Criminal_Minds=["Mystery", "Drama"]
Avengers=["Action", "Fantasy", "Sci-Fi"]
Scooby_doo=["Comedy", "Horror"]
Brooklyn_99=["Action", "Comedy" ]
The_fault_in_our_stars=["Romance", "Drama"]
The_tomorrow_war=["Drama", "Sci-Fi"]
Maze_Runner=["Drama", "Dystopian", "Thriller"]
Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
Harry_Potter=["Mystery", "Fantasy"]
Fast_Furious=["Action"]
Conjuring=["Horror"]
Fantastic_Beast=["Fantasy"]
Parasite=["Comedy", "Thriller"]
Space_between_us=["Romance"]
Murder_Mystery=["Romance", "Mystery"]
The_Purge=["Dystopian", "Thriller"]
Spider_Man=["Action", "Fantasy"]
Dune=["Action", "Dystopian"]
Terminator=["Action", "Sci-Fi"]
Tom_and_Jerry=["Comedy"]
Dolittle=["Comedy", "Fantasy"]

variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war', 
'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite', 
'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle'] 

newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war, 
Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]

def ask():
    user_genre = (input("What movie/show genre do you like to watch?: "))
    user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")

    movies_found = 0
    print("The following movies were found for the genres" + str(user_genre))
    for movie in newlist:
        for genre in user_genre:
            if genre in movie:
                print(variable_strings[newlist.index(movie)].replace("_"," "))
                movies_found += 1

    if movies_found == 0:
        print("No match was found")

def stop_running():
    while user_genre != "Stop":
        ask()
    if user_genre == "Stop":
        print("Stopping")

stop_running()

原文链接:https://stackoverflow.com//questions/71476676/namererror-name-was-not-defined-python

回复

我来回复
  • Abhyuday Vaish的头像
    Abhyuday Vaish 评论

    这是因为您正在定义user_genreinask()函数。在运行ask()函数之前您已经在使用user_genre。您可以删除stop_running()函数并将该逻辑应用到ask()函数本身。这可以通过以下方式实现:

    Anabelle=["Horror", "Triller"]
    Criminal_Minds=["Mystery", "Drama"]
    Avengers=["Action", "Fantasy", "Sci-Fi"]
    Scooby_doo=["Comedy", "Horror"]
    Brooklyn_99=["Action", "Comedy" ]
    The_fault_in_our_stars=["Romance", "Drama"]
    The_tomorrow_war=["Drama", "Sci-Fi"]
    Maze_Runner=["Drama", "Dystopian", "Thriller"]
    Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
    Harry_Potter=["Mystery", "Fantasy"]
    Fast_Furious=["Action"]
    Conjuring=["Horror"]
    Fantastic_Beast=["Fantasy"]
    Parasite=["Comedy", "Thriller"]
    Space_between_us=["Romance"]
    Murder_Mystery=["Romance", "Mystery"]
    The_Purge=["Dystopian", "Thriller"]
    Spider_Man=["Action", "Fantasy"]
    Dune=["Action", "Dystopian"]
    Terminator=["Action", "Sci-Fi"]
    Tom_and_Jerry=["Comedy"]
    Dolittle=["Comedy", "Fantasy"]
    
    variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war', 
    'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite', 
    'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle'] 
    
    newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war, 
    Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]
    
    
    
    def ask():
        while True:
            user_genre = input("What movie/show genre do you like to watch?: ")
            if user_genre != "Stop":
                user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
    
                movies_found = 0
                print("The following movies were found for the genres" + str(user_genre))
                for movie in newlist:
                    for genre in user_genre:
                        if genre in movie:
                            print(variable_strings[newlist.index(movie)].replace("_"," "))
                            movies_found += 1
    
                if movies_found == 0:
                    print("No match was found")
            else:
                print("stopping")
                break
    
    
    ask()
    

    输出:

    What movie/show genre do you like to watch?: Horror
    The following movies were found for the genres['Horror']
    Anabelle
    Scooby doo
    Conjuring
    What movie/show genre do you like to watch?: Stop
    stopping
    
    2年前 0条评论
  • MATTHEW PATTOK的头像
    MATTHEW PATTOK 评论

    这里的主要问题是范围。您在函数中定义的任何变量都将具有所谓的局部范围,这意味着它仅存在于该函数中。每次调用都会定义它,并且在每次调用结束时它将不复存在。在任何函数之外定义的变量都是全局变量,这意味着它们无处不在。
    请注意,如果您在函数中定义与全局变量同名的变量,则不会影响全局变量。它创建一个同名的新变量。

    这里出现的地方是你试图调用user_genreinstop_running而那个变量只存在于ask。如果你想让user_genre被两个函数使用,你需要让它成为全局的。你必须在两个函数中将它声明为全局使用global关键字——用这个关键字定义的变量在任何地方都是全局的。请注意,在定义变量之前,必须在两行单独的行中将变量声明为全局,例如:

    global a
    a = 2
    

    在将其声明为全局之后,您需要在stop_running中定义它,因为您在该函数中要求它提供一个值;在第一次调用ask之前它需要有一个值。

    最后一个问题是在askyousplituser_genre,把它变成一个列表而不是一个字符串。然后当stop_running检查它对"Stop"它总是会返回False因为如果用户输入Stopthenuser_input不会以"Stop"结束,而是作为["Stop"]。您可以通过使用in关键字检查 ifuser_genrecontains"Stop"而不是检查是否相等来解决此问题。因此您的函数将如下所示:

    def ask():
        global user_genre
        user_genre = (input("What movie/show genre do you like to watch?: "))
        ...
    
    def stop_running():
        global user_genre
        user_genre = ''
        while not "Stop" in user_genre:
            ask()
        if "Stop" in user_genre:
            print("Stopping")
    

    请注意,当用户输入Stop时,它仍然会检查电影,您可以通过放置

    if "Stop" in user_genre:
        return
    

    ask检查电影之前。
    然后通过所有这些更改,脚本如下所示:

    Anabelle=["Horror", "Triller"]
    Criminal_Minds=["Mystery", "Drama"]
    Avengers=["Action", "Fantasy", "Sci-Fi"]
    Scooby_doo=["Comedy", "Horror"]
    Brooklyn_99=["Action", "Comedy" ]
    The_fault_in_our_stars=["Romance", "Drama"]
    The_tomorrow_war=["Drama", "Sci-Fi"]
    Maze_Runner=["Drama", "Dystopian", "Thriller"]
    Hunger_Game=["Dystopian", "Sci-Fi", "Thriller"]
    Harry_Potter=["Mystery", "Fantasy"]
    Fast_Furious=["Action"]
    Conjuring=["Horror"]
    Fantastic_Beast=["Fantasy"]
    Parasite=["Comedy", "Thriller"]
    Space_between_us=["Romance"]
    Murder_Mystery=["Romance", "Mystery"]
    The_Purge=["Dystopian", "Thriller"]
    Spider_Man=["Action", "Fantasy"]
    Dune=["Action", "Dystopian"]
    Terminator=["Action", "Sci-Fi"]
    Tom_and_Jerry=["Comedy"]
    Dolittle=["Comedy", "Fantasy"]
    
    variable_strings=['Anabelle', 'Criminal_Minds', 'Avengers', 'Scooby_doo', 'Brooklyn_99', 'The_fault_in_our_stars', 'The_tomorrow_war', 
    'Maze_Runner', 'Hunger_Game', 'Harry_Potter', 'Fast_Furious', 'Conjuring', 'Fantastic_Beast', 'Parasite', 
    'Space_between_us', 'Murder_Mystery', 'The_Purge', 'Spider_Man', 'Dune', 'Terminator', 'Tom_and_Jerry', 'Dolittle'] 
    
    newlist = [Anabelle, Criminal_Minds, Avengers, Scooby_doo, Brooklyn_99, The_fault_in_our_stars, The_tomorrow_war, 
    Maze_Runner, Hunger_Game, Harry_Potter, Fast_Furious, Conjuring, Fantastic_Beast, Parasite, Space_between_us, Murder_Mystery, The_Purge, Spider_Man, Dune, Terminator, Tom_and_Jerry, Dolittle]
    
    def ask():
        global user_genre
        user_genre = (input("What movie/show genre do you like to watch?: "))
        user_genre = user_genre.split(", ") if ", " in user_genre else user_genre.split(",")
        if "Stop" in user_genre:
            return
        movies_found = 0
        print("The following movies were found for the genres" + str(user_genre))
        for movie in newlist:
            for genre in user_genre:
                if genre in movie:
                    print(variable_strings[newlist.index(movie)].replace("_"," "))
                    movies_found += 1
    
        if movies_found == 0:
            print("No match was found")
    
    def stop_running():
        global user_genre
        user_genre = ''
        while not "Stop" in user_genre:
            ask()
        if "Stop" in user_genre:
            print("Stopping")
    
    stop_running()
    
    2年前 0条评论