【发布时间】:2020-09-15 21:12:08
【问题描述】:
我需要实现一个名为“verify”的函数,它接受一个参数 称为“数字”,然后检查以下规则:
- 第一个数字必须是 4。
- 第四位必须比第五位大一;请记住,这些 由于格式为####-####-####,因此用破折号分隔。
- 所有数字之和必须能被 4 整除。
- 如果将前两位视为两位数,第七位和第八位 两位数的数字,它们的总和必须是 100
这是我目前想出的:
def verify(number): # do not change this line!
list_number=list(number.split(''))
Check=false
# write your code here so that it verifies the card number
if list_number[0]==4:
if list_number[3]==list_number[5]+1:
if list_number.sum() % 4==0:
if int(str(list_number[0])+str(list_number[1]))+int(str(list_number[7])+str(list_number[8])) ==100:
Check = true
else:
check = false
input = "4094-3460-2754" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output
【问题讨论】:
-
除了不确定我的代码是否正确之外,我还收到此错误:“第 8 行错误:如果 list_number[0]==4: ^ IndentationError: unexpected indent”
-
您的 else 没有正确缩进,并且您的 if 语句都没有正文.....您的代码远非可运行。您的 ifs 也比之前的代码缩进更多。
标签: python validation numbers rules credit-card