我有一个下载视频文件的程序这里是完整的,不用担心它是一个简短的程序。
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 声明为全局变量。有没有办法在另一个函数中使用一个函数中的变量?