私はビデオファイルをダウンロードするプログラムを持っています。ここではフルですが、短いプログラムを心配しないでください。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()
私はより小さな機能に分解したいと思います。私はこれをやろうとしました:
しかし、これを試してみると、私は最高のものが宣言されていないと言っているエラーが出ます。私はグローバル変数としてベストを宣言したくありません。ある関数の変数を別の関数の中で使用する方法はありますか?
メソッドの解決順序を表示できますか?つまり、 'download()'がどこから呼び出され、いつ、 'url()'と同じであるかということです。 2番目の例でも完全なプログラムを提供するだけです。 – light2yellow
はい、ある関数の変数を別の関数の中で使用する方法があります。 *パラメータ*を使用してください。 – light2yellow
@ light2yellowありがとう、私はパラメータを使用しようとしましたが、私はそれを正しく行っていなかったので、ここに来たのです。私は両方のurl()を呼び出してファイルの末尾にダウンロードします –