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

时间:2012-12-28 20:31:15

标签: python python-3.x

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

2 个答案:

答案 0 :(得分:4)

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

httpurl = str(httpurl)

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

行[{1}}和mp3 = int(mp3)也是如此,但 需要在连接时将ext = string(ext)强制转换为字符串:

mp3

虽然如果你改用字符串格式会更好:

final = httpurl+str(mp3)+ext

答案 1 :(得分: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