對於每個數字,它必須將該數字乘以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. –