Ich habe ein Programm, das Videodateien herunterlädt Hier ist es in vollem Umfang, keine Sorge, es ist ein kurzes Programm.Verwenden Sie eine Variable aus einer Funktion innerhalb einer anderen Funktion mit 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()
Ich möchte es in kleinere Funktionen aufteilen. Ich habe versucht, dies zu tun:
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()
Aber wenn ich versuche, diese bekomme ich einen Fehler mir zu sagen, dass am besten nicht erklärt. Ich möchte nicht als globale Variable am besten deklarieren. Gibt es eine Möglichkeit, eine Variable aus einer Funktion in einer anderen Funktion zu verwenden?
Können Sie die Reihenfolge Ihrer Methodenauflösung anzeigen? Ich meine, von wo 'download()' aufgerufen wird und wann dasselbe über 'url()'. Stellen Sie auch im zweiten Beispiel ein vollständiges Programm bereit. – light2yellow
Und ja, es gibt eine Möglichkeit, eine Variable aus einer Funktion in einer anderen Funktion zu verwenden. Verwenden Sie einen * Parameter *. – light2yellow
@ light2yellow Danke, ich habe versucht, einen Parameter zu verwenden, aber ich habe es nicht richtig gemacht, deshalb bin ich hierher gekommen. Ich rief beide URL() und downloaden Sie am Ende der Datei –