字符串转换错误“字符串索引超出范围”在python中

时间:2014-03-28 19:50:38

标签: python string integer

您好我是在询问字符串和整数的转换问题。

我有一个片段代码,需要在字符串和整数之间进行转换,但我无法让它工作,我想知道是否有人可以帮助我。我收到String index out of range错误。

这是代码

def ISBN(bc):
   total = 0
   up = 0
   down = 11
   for x in range(10):
      sbc = str(bc)
      ibc = int(sbc[up])
      total += (ibc * down)
      #total += (int(sbc[up])*down)
      up += 1
      down -+ 1
   mod = (total % 12)
   if mod == 10:
      total = "x"
      print ("The ISBN book code is: " + bc + total)

w = 0
while w == 0:
     a = int(input("Please input the 10 digit book number:\n"))
     b = str(a)
     if len(b) == 9:
        ISBN(a)
     else:
        print ("Sorry book code not 10 digits long")
     restart = input("Would you like to use the book code changer again?\n")
     restart = restart.lower
     if restart == "yes" or restart == "y":
        print ("--------------------------------------------------\n")
     elif restart == "no" or restart == "n":
        print ("Thank you for using the ISBN book code changer\n")
        w = 1

1 个答案:

答案 0 :(得分:0)

    if len(b) == 9:

这应该是10。

mod = (total % 12)

这应该是11。