2016-08-18 63 views
0

我有一個程序下載視頻文件在這裏它是完整的,不用擔心它的短程序。使用Python中的另一個函數中的一個函數的變量

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() 

但是,當我嘗試這個,我得到一個錯誤告訴我,最好還沒有宣佈。我不想宣佈最好的全局變量。有沒有一種方法使用另一個函數內的一個函數的變量?

+0

你能顯示你的方法解析順序嗎?我的意思是,從何處調用'download()'以及何時,如何調用'url()'。在第二個例子中也要提供一個完整的程序。 – light2yellow

+1

是的,有一種方法可以從另一個函數內的一個函數中使用一個變量。使用*參數*。 – light2yellow

+0

@ light2yellow謝謝,我確實嘗試使用一個參數,但是我沒有正確地做到這一點,所以我來到這裏。我調用了url()並在文件末尾下載 –

回答

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() 

我認爲這兩種解決方案會給你想要的東西,但我會建議先在這種特殊情況下,因爲它不SE他們像一個複雜的程序。

0

將一個大功能分解爲小功能是一種好習慣,但更迫切的問題是您應該使用主循環並使您的函數返回而不是像這樣鏈接它們。 () - > another_download() - >下載() - > another_download() - >下載() - > ...,所以如果用戶想下載n vids,你將有n * 2 - 1功能掛起,直到最後一個完成。

順便回報解決您的問題:

def url(): 
    ... 
    return best 

def download(): 
    best = url() 
    ... 
相關問題