我有一個程序下載視頻文件在這裏它是完整的,不用擔心它的短程序。使用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()
但是,當我嘗試這個,我得到一個錯誤告訴我,最好還沒有宣佈。我不想宣佈最好的全局變量。有沒有一種方法使用另一個函數內的一個函數的變量?
你能顯示你的方法解析順序嗎?我的意思是,從何處調用'download()'以及何時,如何調用'url()'。在第二個例子中也要提供一個完整的程序。 – light2yellow
是的,有一種方法可以從另一個函數內的一個函數中使用一個變量。使用*參數*。 – light2yellow
@ light2yellow謝謝,我確實嘗試使用一個參數,但是我沒有正確地做到這一點,所以我來到這裏。我調用了url()並在文件末尾下載 –