我如何让它询问用户是否要转换另一个视频?
How do I make it ask the user if they wish to convert another video?
我有以下代码,这是一个简单的 YouTube 到 MP4 转换器,但我想实现询问用户是否要转换另一个视频的功能,而不仅仅是“按按钮退出”。 =11=]
from pytube import YouTube
from pytube.cli import on_progress
from click import pause
# ask for the link from user
link = input("Enter the link of the YouTube video you want to download: ")
yt = YouTube(link, on_progress_callback=on_progress)
print("\n---------------------Video Details---------------------------------------")
# Showing details
print("\nTitle: ", yt.title, "\n")
print("Number of views: ", yt.views, "\n")
print("Length of video: ", yt.length, "\n")
print("Rating of video: ", yt.rating, "\n")
print("-----------------------Video Download---------------------------------------")
# Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()
# Starting download and exiting
print("\n")
pause("Press any key to download...")
print("Downloading...")
ys.download()
print("Download completed!!")
print("\n")
pause("Thank you! Press any key to exit...")`
将整个代码放在一个while True
中,在过程结束时,要求用户转换另一个视频,如果用户说no
,则中断循环。
最终代码应该是这样的:
while True:
"""
Your codes
"""
user_input = input("Do you want to convert another video? [y/n]")
if user_input not in ["y", "yes", "Y", "Yes"]:
break
这是想法的简单实现!:)
也许你会带来更好的!
在这种情况下,您可以做的是创建一个包含您的代码的 while 循环,然后您可以询问用户是否希望继续。
您可以像要求用户给您提供 youtube link 一样,使用输入。然后你可以检查答案是是还是否,如果他们不想继续退出 while 循环并完成你的程序。
一个例子是:
while True:
# Download video code
wish_to_continue = input("Do you want to continue with another video?")
if wish_to_continue == "no":
# exit the while loop and the program
break
我有以下代码,这是一个简单的 YouTube 到 MP4 转换器,但我想实现询问用户是否要转换另一个视频的功能,而不仅仅是“按按钮退出”。 =11=]
from pytube import YouTube
from pytube.cli import on_progress
from click import pause
# ask for the link from user
link = input("Enter the link of the YouTube video you want to download: ")
yt = YouTube(link, on_progress_callback=on_progress)
print("\n---------------------Video Details---------------------------------------")
# Showing details
print("\nTitle: ", yt.title, "\n")
print("Number of views: ", yt.views, "\n")
print("Length of video: ", yt.length, "\n")
print("Rating of video: ", yt.rating, "\n")
print("-----------------------Video Download---------------------------------------")
# Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()
# Starting download and exiting
print("\n")
pause("Press any key to download...")
print("Downloading...")
ys.download()
print("Download completed!!")
print("\n")
pause("Thank you! Press any key to exit...")`
将整个代码放在一个while True
中,在过程结束时,要求用户转换另一个视频,如果用户说no
,则中断循环。
最终代码应该是这样的:
while True:
"""
Your codes
"""
user_input = input("Do you want to convert another video? [y/n]")
if user_input not in ["y", "yes", "Y", "Yes"]:
break
这是想法的简单实现!:) 也许你会带来更好的!
在这种情况下,您可以做的是创建一个包含您的代码的 while 循环,然后您可以询问用户是否希望继续。 您可以像要求用户给您提供 youtube link 一样,使用输入。然后你可以检查答案是是还是否,如果他们不想继续退出 while 循环并完成你的程序。
一个例子是:
while True:
# Download video code
wish_to_continue = input("Do you want to continue with another video?")
if wish_to_continue == "no":
# exit the while loop and the program
break