未定义定义名称Python

li9yvcax  于 2022-12-24  发布在  Python
关注(0)|答案(3)|浏览(212)

我是新的python,并开始了一个项目对我自己,长话短说,我试图编程一个电影选择器通过刮取从imdb取决于流派,到目前为止一切顺利,除了从第二def称为恐怖()是给我一个错误说没有定义,任何帮助将非常感谢。:)
这是我代码

import random
import secrets
import requests
from bs4 import BeautifulSoup
import numpy as np

def main():

    print("1 Horror.")
    print('2 Romance.')
    print("3 Sci-Fi.")
    print("4 Comedy.")
    print("0 Exit.")
    menu()

def menu():
    while True:
        choice = int(input("Enter your choice; "))

        if choice == 1:
                print("Horror?")
                horror()
        elif choice == 2:
                print("Romance?")
        elif choice == 3:
                print("Comedy?")
                comedy()
        elif choice == 4:
                print("Sci-Fi?")
        elif choice == 0:
                print("Goodbye!")
                break
        else:
            print("Invalid choice.")

def comedy():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title/?genres=comedy&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_1')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            name = container.h3.a.text
            titles.append(name)
            ComedyMovie = (secrets.choice(titles))
        print("")
        print(ComedyMovie)
        print("")
        np.delete
        break
main()

def horror():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title?genres=horror&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_3')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            # Scraping the movie's name
            name = container.h3.a.text
            titles.append(name)
            HorrorMovie = (secrets.choice(titles))
        print("")
        print(HorrorMovie)
        print("")
        np.delete
        break
main()

基本上喜剧类型的作品很好,恐怖不那么好

ohtdti5x

ohtdti5x1#

这里发生的事情是,当文件启动时,它逐行读取所有代码,直到第一次调用main()方法,然后立即开始执行该方法,挂起对文件的进一步阅读。
在这主要()方法,则有另一个方法菜单()。一旦您将输入作为恐怖提供,系统将尝试运行恐怖()方法,但它无法找到它,因为在引导代码时读取的最后一行是第一个main(),并且它无法继续超过该点以进行缓冲,因为它被该方法阻塞。你不能调用函数horror(),因为从技术上讲,它是在定义之前被调用的。
在python中,你可以在文件的任何地方定义一个方法,并以任何顺序调用它们,但是你不能在定义之前调用它们。
因此,就像@user2357112建议的那样,只需要删除第一个main()方法,它就会在文件的末尾被调用,最好是使用if __name__=='__main__',然后一切都会按预期工作。

eni9jsuy

eni9jsuy2#

尝试在脚本中使用if __name__ == __main__:,这样可以调用脚本并执行其中的所有代码,而不是像模块调用那样执行函数。

import random
import secrets
import requests
from bs4 import BeautifulSoup
import numpy as np

def main():

    print("1 Horror.")
    print('2 Romance.')
    print("3 Sci-Fi.")
    print("4 Comedy.")
    print("0 Exit.")
    menu()

def menu():
    while True:
        choice = int(input("Enter your choice; "))

        if choice == 1:
                print("Horror?")
                horror()
        elif choice == 2:
                print("Romance?")
        elif choice == 3:
                print("Comedy?")
                comedy()
        elif choice == 4:
                print("Sci-Fi?")
        elif choice == 0:
                print("Goodbye!")
                break
        else:
            print("Invalid choice.")

def comedy():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title/?genres=comedy&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_1')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            name = container.h3.a.text
            titles.append(name)
            ComedyMovie = (secrets.choice(titles))
        print("")
        print(ComedyMovie)
        print("")
        np.delete
        break

def horror():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title?genres=horror&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_3')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            # Scraping the movie's name
            name = container.h3.a.text
            titles.append(name)
            HorrorMovie = (secrets.choice(titles))
        print("")
        print(HorrorMovie)
        print("")
        np.delete
        break

if __name__ == "__main__":
  main()
5ssjco0h

5ssjco0h3#

请试用此代码

import random
import secrets
import requests
from bs4 import BeautifulSoup
import numpy as np

def horror():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title?genres=horror&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_3')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            # Scraping the movie's name
            name = container.h3.a.text
            titles.append(name)
            HorrorMovie = (secrets.choice(titles))
        print(f"\n{HorrorMovie}\n")
        np.delete
        break

def comedy():
    titles = []
    pages = np.arange(1, 1001, 50)
    pages
    for page in pages:
        page = requests.get(
            'https://www.imdb.com/search/title/?genres=comedy&explore=title_type,genres&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=3396781f-d87f-4fac-8694-c56ce6f490fe&pf_rd_r=WWP23JMS19Y0FJQ181DT&pf_rd_s=center-1&pf_rd_t=15051&pf_rd_i=genre&ref_=ft_gnr_pr1_i_1')
        soup = BeautifulSoup(page.text, 'html.parser')

        movie_div = soup.find_all('div', {'class': 'lister-item mode-advanced'})

        for container in movie_div:
            name = container.h3.a.text
            titles.append(name)
            ComedyMovie = (secrets.choice(titles))
        print(f"\n{ComedyMovie}\n")
        np.delete
        break

def main():
    print("1 Horror.")
    print('2 Romance.')
    print("3 Sci-Fi.")
    print("4 Comedy.")
    print("0 Exit.")
    menu()

def menu():
    while True:
        choice = int(input("Enter your choice; "))

        if choice == 1:
                print("Horror?")
                horror()
        elif choice == 2:
                print("Romance?")
        elif choice == 3:
                print("Comedy?")
                comedy()
        elif choice == 4:
                print("Sci-Fi?")
        elif choice == 0:
                print("Goodbye!")
                break
        else:
            print("Invalid choice.")

main()

相关问题