我需要实现一个名为“ verify”的函数,该函数需要一个参数称为“数字”,然后检查以下规则:
这是我到目前为止提出的:
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
这就是您所需要的。函数中的每个if语句都与您指定的条件相对应。
def verify(number): # do not change this line!
number_string = number.replace("-", "")
list_number = [int(n) for n in number_string]
if not list_number[0] == 4:
return False
if not list_number[3] - list_number[4] == 1:
return False
if not sum(list_number) % 4 == 0:
return False
if not int(number_string[0:2]) + int(number_string[6:8]) == 100:
return False
return True
input = "4094-3460-2754"
output = verify(input)
print(output)
# True
您的代码是写得不好的方法
list_number = "4094-3460-2754"
list_number = list_number.replace('-','')
if list_number[0]=='4' and int(list_number[3])==int(list_number[5])+1 and list_number.sum() % 4==0 and int(list_number[0])+int(list_number[1])==int(list_number[7])+int(list_number[8]):
check = True
else:
check = False
似乎您的代码中存在一些基本错误。我会为您列出它们:
布尔值以大写字母开头
if和else语句后没有缩进
您的输出将始终不打印任何内容,因为您的函数不会返回任何内容。
def verify(number):
list_number=list(number)
if list_number[0]==4 and list_number[3]==list_number[5]+1 and list_number.sum() % 4==0 and int(str(list_number[0])+str(list_number[1]))+int(str(list_number[7])+str(list_number[8])) ==100:
return True
else:
return False
input = "4094-3460-2754"
output = verify(input)
print(output)