对于每个数字,它必须将该数字乘以1
至9
。然后将所有这些值一起添加。通过增加数值相乘每个数字/数字
例如,如果在文件上的一行数为013149498
,它应该是:
0*1 + 1*2 + 3*3 + 1*4 + 4*5 + 9*6 + 4*7 + 9*8 + 8*9
现在我有2
乘以所有数字。
def main():
# Open the isbns.txt file for reading.
isbn_file = open('isbns.txt', 'r')
print ('Here are the ISBNs')
# Get the values from the file, multiply each by 0 to 9, then sum.
for line in isbn_file:
# Check if the length of the line is 10.
size = len(line)
print('ISBN:', size)
if size == 11:
for number in range(0,9):
maths = number * 2
print(number, maths, sep="...")
else:
print('ISBN invalid:', line)
# Close the file.
isbn_file.close()
# Call the main function.
main()
请修复您的indenta并且发布*整个*代码,例如'size'是什么...... –
你的代码说“检查行的长度是否为10”,但你的检查是“size == 11”。 '1-9'只有9位数字。你期望输入行看起来像什么?该操作是否仅适用于前9位数字?你在做ISBN验证吗? –
@PatrickHaugh是的,我正在做ISBN验证。我把大小== 11,因为由于某种原因大小== 10不起作用。我正在通过乘以前9个数字来验证校验位,然后确保总和(261)可以被11整除。然后我验证校验位8被选中,因为261在253和264之间(11的倍数)和261是8超过253. –