gpt4 book ai didi

python - 将 while 循环添加到 try 和 except 语句

转载 作者:行者123 更新时间:2023-12-01 04:47:18 24 4
gpt4 key购买 nike

# import mutagen

from mutagen.easyid3 import EasyID3

from mutagen.mp4 import MP4

from mutagen.mp3 import MP3

# Define a function to read ID3 tags

def readid3mp3 (ip):

audio = MP3(ip)

print(audio['TALB'])
print(audio['TIT2'])
print(audio['TPE1'])
return

在这部分代码中添加 while 循环时遇到问题。正如您所看到的,如果用户没有输入音乐文件,它就会打印出来(“哦,这不是音乐文件”),但如果发生这种情况,我希望程序继续询问,直到用户输入音乐文件。

####### main body ########

# prompt user to enter a MP4

song = input('enter a music file please:')

try:

readid3mp4(song)

except IOError:

print("ooooops this is not a music file")

else:

print("music file stored")

最佳答案

您可以使用 while 循环,在正确阅读歌曲后中断。否则,如果抛出异常,则会打印错误消息并正常继续。

while True:
try:
readid3mp4(song)
except IOError:
print("ooooops this is not a music file")
else:
break

关于python - 将 while 循环添加到 try 和 except 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com