将变量添加到if语句中的变量

时间:2017-11-06 03:02:52

标签: python

我正在尝试将我的函数的数量添加到语句中,但我无法弄明白。这是我的功能:

def hash_func(n):
    number = ("1234567890")
    x = 0
    for each_item in n:   
        if each_item in number:
            x = x + each_item  #this is the line where code goes wrong
    return x
print(hash_func("ironman3"))

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您正在尝试添加带整数的字符串字符。 将值转换为数字数据类型,它将起作用。

x = x + int(each_item)