NamerError:名称未定义python
原文标题 :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()
回复
我来回复-
Abhyuday Vaish 评论
这是因为您正在定义
user_genre
inask()
函数。在运行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年前 -
MATTHEW PATTOK 评论
这里的主要问题是范围。您在函数中定义的任何变量都将具有所谓的局部范围,这意味着它仅存在于该函数中。每次调用都会定义它,并且在每次调用结束时它将不复存在。在任何函数之外定义的变量都是全局变量,这意味着它们无处不在。
请注意,如果您在函数中定义与全局变量同名的变量,则不会影响全局变量。它创建一个同名的新变量。这里出现的地方是你试图调用
user_genre
instop_running
而那个变量只存在于ask
。如果你想让user_genre
被两个函数使用,你需要让它成为全局的。你必须在两个函数中将它声明为全局使用global
关键字——用这个关键字定义的变量在任何地方都是全局的。请注意,在定义变量之前,必须在两行单独的行中将变量声明为全局,例如:global a a = 2
在将其声明为全局之后,您需要在
stop_running
中定义它,因为您在该函数中要求它提供一个值;在第一次调用ask
之前它需要有一个值。最后一个问题是在
ask
yousplit
user_genre
,把它变成一个列表而不是一个字符串。然后当stop_running
检查它对"Stop"
它总是会返回False
因为如果用户输入Stop
thenuser_input
不会以"Stop"
结束,而是作为["Stop"]
。您可以通过使用in
关键字检查 ifuser_genre
contains"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年前