IBAN检查错误

时间:2016-11-20 15:46:40

标签: python

我对python很新,而且我正在为学校做一件额外的事情。 我的老师要求我们制作一个IBAN发电机,但我无法弄清楚我做错了什么(可能是很多东西),但有人可以帮忙吗?

我的代码:

letter_dic = {"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15, "G": 16, "H": 17, "I": 18, "J": 19, "K": 20,
          "L": 21, "M": 22, "N": 23, "O": 24, "P": 25, "Q": 26, "R": 27, "S": 28, "T": 29, "U": 30, "V": 31,
          "W": 32, "X": 33, "Y": 34, "Z": 35}

def move_digits_to_back(iban_string):
    x = iban_string[0:4]
    y = iban_string[4:]
    iban_string = y + x
    return iban_string

def convert_characters_to_digits(iban_back):
    iban_num = (iban_string)[4:][:4].translate(letter_dic)
    print iban_num
    return int(iban_num)

def is_modulo_correct(iban_num):
    if iban_num % 97 != 1:
        return True
    return False

def validate_iban(iban_string):
    move_digits_to_back(iban_string)
    convert_characters_to_digits(iban_string)
    if is_modulo_correct(iban_num) is True:
        print("IBAN-nummer {} is correct".format(iban_string))
    else:
        print("IBAN-Nummer {} is niet correct".format(iban_string))

while True:
    iban_string = raw_input("Vul een IBAN-nummer in om te controleren.\n")
    validate_iban(iban_string)

错误

    line 13, in convert_characters_to_digits
    iban_num = (iban_string)[4:][:4].translate(letter_dic)
TypeError: expected a string or other character buffer object

提前致谢。

1 个答案:

答案 0 :(得分:0)

问题是你只能使用loadChildren进行一对一映射,在你的情况下,你有一个字母映射到两个数字。以下是修复该代码的代码版本。此外,您忘记获得translate的返回值。以下是解决这些问题的代码版本:

convert_characters_to_string