将乘法序列转换为循环?

时间:2014-01-28 22:06:44

标签: python

任何人都可以将循环包含在此代码中:

ISBN=input('Please enter the 10 digit number: ')

while len(ISBN)!= 10:
print('Please make sure you have entered a number which is exactly 10 characters long.')
ISBN=int(input('Please enter the 10 digit number: '))
continue
else:
    Digit1=int(ISBN[0])*11
    Digit2=int(ISBN[1])*10
    Digit3=int(ISBN[2])*9
    Digit4=int(ISBN[3])*8
    Digit5=int(ISBN[4])*7
    Digit6=int(ISBN[5])*6
    Digit7=int(ISBN[6])*5
    Digit8=int(ISBN[7])*4
    Digit9=int(ISBN[8])*3
    Digit10=int(ISBN[9])*2
    Sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)
    Mod=Sum%11
    Digit11=11-Mod
    if Digit11==10:
       Digit11='X'
    ISBNNumber=str(ISBN)+str(Digit11)
    print('Your 11 digit ISBN Number is ' + ISBNNumber)

3 个答案:

答案 0 :(得分:2)

sum(int(v)*(11-i) for i,v in enumerate(ISBN))

答案 1 :(得分:1)

for i in xrange(11):
    sum+=ISBN[i]*(11-i)

答案 2 :(得分:1)

一个好的,单行的怎么样:

Sum = sum(d*i for d,i in zip(ISBN, range(11,0,-1)))