-3

这是一些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

错误:

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    httpurl = string(httpurl)
NameError: name 'string' is not defined

这是在线的:http: //ideone.com/u0ZBo2

4

2 回答 2

4

string不是内置的python类型。您可能想要str()

httpurl = str(httpurl)

但这是多余的,因为httpurl已经type str()

mp3 = int(mp3)和 也是如此ext = string(ext),但在连接时需要转换mp3为字符串:

final = httpurl+str(mp3)+ext

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

final = '{0}{1}{2}'.format(httpurl, mp3, ext)
于 2012-12-28T20:33:59.110 回答
3

首先,它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
于 2012-12-28T20:35:56.867 回答