2016-07-30 110 views
-1

我對python非常陌生,並且真的停留在此。python:代碼中的列表索引超出範圍錯誤

基本上,我應該做一個檢查代碼來檢查NRIC的最後一個字母。只要有7個數字(比如應該有的),我的代碼就可以正常工作。然而,我的老師只是幫助我發現,只要數字以0開頭,我的代碼就無法工作。下面是我的代碼。

def check_code(): 
    nricno = int(input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will fail.")) 

    NRIC = [ int(x) for x in str(nricno) ] 

    a = NRIC[0]*2 
    b = NRIC[1]*7 
    c = NRIC[2]*6 
    d = NRIC[3]*5 
    e = NRIC[4]*4 
    f = NRIC[5]*3 
    g = NRIC[6]*2 

    SUM = int(a + b + c + d + e + f +g) 

    remainder = int(SUM % 11) 
    leftovers = int(11 - remainder) 

    rightovers = leftovers - 1 

    Alphabet = "ABCDEFGHIZJ" 

    checkcode = chr(ord('a') + rightovers) 

    print(checkcode) 

check_code() 

這是在下圖中應該計算NRIC的方式。

NRIC calculation help.

+0

什麼是 '一個數字,從0開始' 的意思嗎? – Julien

+0

如果鏈接的圖像是你的實際任務,那麼你做得不好。您需要編寫一個將NRIC字符串作爲參數的函數。你的函數不應該調用'input()'函數,它需要返回校驗碼。您發佈的代碼中的邏輯確實_not_將檢查號碼正確轉換爲檢查代碼。 –

回答

2

當轉換的字符串輸入到int,前導零被剝離(例如"0153444" - >153444)。當您在列表理解中再次轉換爲字符串時,您不會返回零,因此您最終得到[1,5,3,4,4,4]的NRIC列表,而不是[0,1 ,5,3,4,4,4]。如果您像這樣刪除int呼叫,則不會丟失前導零。

# Change this: 
nricno = int(input("Please enter your NRIC(numbers only)...")) 
# To this: 
nricno = input("Please enter your NRIC(numbers only)...") 
+0

代碼還有另一個值得指出的問題:如果某人不輸入數字會怎樣?如果他們輸入「ZZZZZZZ」或類似的東西呢? (這顯然是作業。) – user268396

+0

代碼將失敗 –

+2

@YeoZheYong我認爲這是user268396的聲明點。這是一個額外的輸入驗證級別,你可能想要做... :)檢查他們已經輸入完全7個字符,並且在嘗試任何事情之前他們都是數字... –

0

編輯:此代碼驗證輸入是否由7位數組成。

def check_code():  
    while True: 

     nricno = input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will restart.") 

     if len(nricno) == 7 and nricno.digits == True: 
      print ("Works") 
      continue 
     else: 
      print("Error, 7 digit number was not inputted and/or letters and other characters were inputted.") 


    a = NRIC[0]*2 
    b = NRIC[1]*7 
    c = NRIC[2]*6 
    d = NRIC[3]*5 
    e = NRIC[4]*4 
    f = NRIC[5]*3 
    g = NRIC[6]*2 

    SUM = int(a + b + c + d + e + f +g) 

    remainder = int(SUM % 11) 

    print(remainder) 

    leftovers = int(11 - remainder) 

    rightovers = leftovers - 1 

    Alphabet = "ABCDEFGHIZJ" 

    checkcode = chr(ord('a') + rightovers) 

    print(checkcode.upper()) 

check_code() 
+1

嗯......你想'break'而不是'continue'在那裏,否則它只會打印完成,並且即使在有效輸入上也會再次循環...另外,檢查應該是'if len(nricno)== 7和nricno.isdigit()' –

+0

在我的Python IDLE上,這個版本雖然 –

1

下面是一個緊湊的方法來計算身份證檢查代碼。如果將一個無效的字符串傳遞給函數,則會引發ValueError異常,這會導致程序崩潰。如果傳遞了非字符串,則會引發TypeError。您可以使用try:... except語法來捕獲異常。

def check_code(nric): 
    if len(nric) != 7 or not nric.isdigit(): 
     raise ValueError("Bad NRIC: {!r}".format(nric)) 

    weights = (2, 7, 6, 5, 4, 3, 2) 
    n = sum(int(c) * w for c, w in zip(nric, weights)) 
    return "ABCDEFGHIZJ"[10 - n % 11] 

# Test 
nric = "9300007" 
print(check_code(nric)) 

輸出

B 
+0

Wow。不錯的代碼。 –

0

當你強迫你的輸入作爲一個int,領先的0將被解釋爲錯誤在Python 3。例如,int(0351)不會產生任何0351351但只會導致錯誤,說明invalid token

你不應該強制輸入是一個整型,而是添加一個聲明語句,聲明輸入的值必須是一個7位數的整數(或者如果你喜歡,你可以做一個常規語句)。

更改此:

nricno = int(input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will fail."))

要這樣:

nricno = input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will fail.")