【发布时间】:2016-07-30 11:46:35
【问题描述】:
我对 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 的方式,如下图所示。
【问题讨论】:
-
“以0开头的数字”是什么意思?
-
如果链接的图像是您的实际任务,那么您没有正确执行。您需要编写一个将 NRIC 字符串作为参数的函数。你的函数不应该调用
input()函数,它需要返回校验码。您发布的代码中的逻辑确实没有将支票号码正确地转换为支票代码。
标签: python