python if子句获胜';t激活

python if子句获胜';t激活,python,if-statement,Python,If Statement,我正在写一个程序来查找ISBN号码中丢失的数字。你必须找到前9个数字中的11个乘以后的余数:第一个*10,第二个*9,第三个*8,等等。如果余数等于第10位,则ISBN是正确的 我有一些代码,在问号的空格中放一个零,然后在零所在的数字中找到索引,然后将索引加1*到总数中,并将其mod 11。然后,如果带11的剩余部分不等于第10位数字,它将尝试索引2*并继续运行,直到找到正确的数字。然而,完成这最后一部分的代码块没有触发,我也不知道为什么 def calculate_missing(x):

我正在写一个程序来查找ISBN号码中丢失的数字。你必须找到前9个数字中的11个乘以后的余数:第一个*10,第二个*9,第三个*8,等等。如果余数等于第10位,则ISBN是正确的

我有一些代码,在问号的空格中放一个零,然后在零所在的数字中找到索引,然后将索引加1*到总数中,并将其mod 11。然后,如果带11的剩余部分不等于第10位数字,它将尝试索引2*并继续运行,直到找到正确的数字。然而,完成这最后一部分的代码块没有触发,我也不知道为什么

def calculate_missing(x):
    tally=1
    for item in x:
        if item=='?':
            break
        elif item!='?':
            tally+=1
    if tally==10:
        without=str(x).replace('?','')
        if isbncalculate(without)==10:
            return 'x'
        elif isbncalculate(without)==11:
            return '0'
        else:
            return isbncalculate(without)
        #these just find the missing digit if it is the final digit.
    multiply=11-tally
    x=x.replace('?','0')
    lastdig=x[9]
    x=x.replace(x[9],'')
    time=10
    newnum=0
    for item in str(x):
        item=int(item)
        item*=time
        newnum+=int(item)
        time-=1
    otherthing=6
    if lastdig=='x' or lastdig=='X':
        lastdig=10
    elif lastdig=='0':
        lastdig='11'
    final=newnum+(otherthing*multiply)
    answer=final%11
    for item in range(0,10):
        if answer==lastdig:
            #this code won't trigger!
            return otherthing
        else:
            otherthing+=1
最后几行是我正在努力解决的问题。如果我输入
calculate\u missing('567?545653')
,它应该返回6,但它只返回零。

替换:

if answer==lastdig:
与:


问题是,尽管
answer
lastdig
是相等的,但是
answer
是一个整数,而
lastdig
是一个字符串。因此,在比较时将
answer
转换为string,或者将
lastdig
转换为integer。这将解决问题。

这是您使用的实际缩进吗?因为用错误的缩进发布python代码对代码当前的格式没有帮助,
answer
otherthing
、和
lastdig
在for循环的整个过程中都保持不变,这意味着结果永远不会改变!您的代码没有意义-为什么要在
项上循环?如果
回答!=lastdig
在第一次迭代时,它也不会出现在任何后续迭代中。真的吗?我该如何解决这个问题?我认为,因为它们在循环之外,所以它们可以改变。jonrsharpe-答案的一部分是otherthing*乘法,otherthing每次迭代都会改变。我想我把缩进搞错了。非常感谢!它是如此简单,我不敢相信它给我带来了这么多麻烦!我将otherthing更改回0,现在它不再给我结果。我把事情搞砸了吗?然而,我仍然有一个奇怪的问题。我用一个新的isbn 23433234322试了一下,在不同的地方用问号试了一下,得到了这个结果:>>>calculate_missing('2343?34322')>>>>calculate_missing('2?43234322')一个没有给出答案,另一个给出了不正确的答案。现在很困惑!
if answer==int(lastdig):