0

我有一个下载视频文件的程序这里是完整的,不用担心它是一个简短的程序。

import pafy

def download():
    url = raw_input('Please enter the path to the video\n')
    video = pafy.new(url)
    vid_title = video.title
    best = video.getbest()
    streams = video.streams
    print(vid_title)
    for stream in streams:
        print(stream)
    print'Resolution: ',best.resolution,'\nExtension : ', best.extension
    user_choice = raw_input("Do you want to download this video\ny or n\n")
    if user_choice == 'y':
        print'Your video will downloaded soon'
        filename = best.download(filepath = '/home/mark/new_projects')
        another_download()
    elif user_choice =='n':
        print'You have chosen not to download a video'
        another_download()


def another_download():
    another_choice = raw_input('Would you like to download another video\ny or n\n')
    if another_choice == 'y':
        download()
    else:
        print'Thank for using my program'

download()

我想把它分解成更小的函数。我试图这样做:

def url():
    url = raw_input('Please enter the path to the video\n')
    video = pafy.new(url)
    vid_title = video.title
    best = video.getbest()
    streams = video.streams
    print(vid_title)
    for stream in streams:
        print(stream)
    print'Resolution: ',best.resolution,'\nExtension : ', best.extension

def download():
    user_choice = raw_input("Do you want to download this video\ny or n\n")
    if user_choice == 'y':
        print'Your video will downloaded soon'
        filename = best.download(filepath = '/home/mark/new_projects')
        another_download()
    elif user_choice =='n':
        print'You have chosen not to download a video'
        another_download()

但是当我尝试这个时,我得到一个错误,告诉我最好的还没有被宣布。我不想将 best 声明为全局变量。有没有办法在另一个函数中使用一个函数中的变量?

4

2 回答 2

0

将大函数拆分为小函数是一个好习惯,但更紧迫的问题是您应该使用主循环并让您的函数返回,而不是像那样链接它们。

现在 download() -> another_download() -> download() -> another_download() -> download() -> ...,所以如果用户想下载 n 个视频,你将拥有 n * 2 - 1 个函数徘徊,直到最后一个完成。

顺便退货解决了你的问题:

def url():
    ...
    return best 

def download():
    best = url()
    ...
于 2016-08-18T16:04:42.833 回答
0

这里有几个选项供您选择。我会尽力把它们布置好。

选项 1:假设您download()首先调用,您可以url()返回您需要的内容并将其存储在download()方法中的变量中:

def url():
    url = raw_input('Please enter the path to the video\n')
    video = pafy.new(url)
    vid_title = video.title
    best = video.getbest()
    streams = video.streams
    print(vid_title)
    for stream in streams:
        print(stream)
    print'Resolution: ',best.resolution,'\nExtension : ', best.extension
    return best

def download():
    user_choice = raw_input("Do you want to download this video\ny or n\n")
    if user_choice == 'y':
        best = url()
        print'Your video will downloaded soon'
        filename = best.download(filepath = '/home/mark/new_projects')
        another_download()
    elif user_choice =='n':
        print'You have chosen not to download a video'
        another_download()

选项 2:您可以使用全局变量,但我不知道在这种情况下使用它们的后果:

best = None
def url():
    global best
    url = raw_input('Please enter the path to the video\n')
    video = pafy.new(url)
    vid_title = video.title
    best = video.getbest()
    streams = video.streams
    print(vid_title)
    for stream in streams:
        print(stream)
    print'Resolution: ',best.resolution,'\nExtension : ', best.extension

def download():
    global best
    user_choice = raw_input("Do you want to download this video\ny or n\n")
    if user_choice == 'y':
        print'Your video will downloaded soon'
        filename = best.download(filepath = '/home/mark/new_projects')
        another_download()
    elif user_choice =='n':
        print'You have chosen not to download a video'
        another_download()

我认为这些解决方案中的任何一个都可以满足您的需求,但是在这种特定情况下,我会推荐第一个解决方案,因为它看起来并不复杂。

于 2016-08-18T16:05:18.807 回答