在简单的python代码中找不到错误:(

2024-06-26 02:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有一些python3代码,我找不到其中的错误:

httpurl = "http://okgift.ru/audio/Servantes/Don-Kihot/"
httpurl = string(httpurl)
mp3 = 1
mp3 = int(mp3)
ext = ".mp3"
ext = string(ext)
while mp3 < 332:
    final = httpurl+mp3+ext
    print(final)
    mp3 = mp3+1

错误:

^{pr2}$

以下是在线信息: http://ideone.com/u0ZBo2


Tags: 代码httpstringru错误mp3audioext
2条回答

string不是内置的python类型。你想要str()也许:

httpurl = str(httpurl)

但这是多余的,因为httpurl已经是str()类型的。在

对于mp3 = int(mp3)ext = string(ext)这两行也是一样的,但是在连接时,需要将mp3转换为字符串:

^{pr2}$

虽然使用字符串格式会更好:

final = '{0}{1}{2}'.format(httpurl, mp3, ext)

首先,它将是str,而不是转换为字符串类型的字符串。其次,你不需要这些转换。在

httpurl = 'http://www.google.com/' # This is a string, no need to cast
ext = '.mp3' # this is a string, no need to cast
for mp3 in range(1, 332):
    final = "{url}{mp3}{ext}".format(url=httpurl, mp3=mp3, ext=ext)
    print final

相关问题 更多 >