如何在字符串中添加数字

2024-04-24 22:51:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我有一个作业来验证信用卡号码。我完成了前两个条件,但我有3个和4个条件。感谢任何帮助

条件:

  1. 第一个数字必须是4。-完成
  2. 第四位数字必须比第五位大一位;请记住,这些数字之间用破折号分隔,因为格式是####-########################
  3. 所有数字之和必须能被4整除。-需要帮助吗
  4. 如果您将前两位数字视为两位数字,以及第七位和第八位数字 作为两位数,它们的总和必须是100。-需要帮助吗
def verify(number) : # do not change this line!

  # write your code here so that it verifies the card number
  #condtion 1  
  if number[0] != '4':
    return "violates rule #1"

  #condition 2
  if int(number[3]) != (int(number[5]) + 1) :
    return  "violates rule #2"

  #condition 3
  for i in number:
    if i >= '0' and i !='-':

  # be sure to indent your code!

  return True # modify this line as needed

input = "4037-6000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!

预期产出:

^{pr2}$

Tags: thenumberinputoutputyourreturnifline
3条回答

对于条件3

Python中的字符串是iterable,这意味着您可以在for循环中传递它们。循环中的每个元素都是字符串中的一个字符。所以如果你这么做了

for char in "4094-3460-2754":
    print(char)

你会得到:

^{pr2}$

使用这个方法,您可以计算输入中每个数字的和,并检查它是否可以被4整除。需要注意的两件事是,首先需要将字符转换为整数(使用int)。你不能这么做

"4" + "0"

但你能做到

int("4") + int("0")

您还需要使用if从总和中排除“-”。在

其次,我们在Python中使用模(%)检查两个数字是否可以整除。结果是余数,如果余数为0,则第一个参数可被第二个参数整除。在

16 % 4 == 0 # divisible by 4
21 % 4 == 1 # not divisible by 4

对于条件4

Python中的字符串除了可以iterable之外,还可以通过它们的索引(从0开始)来访问它们

"4094-3460-2754"[0] == "4"
"4094-3460-2754"[1] == "0"
"4094-3460-2754"[2] == "9"
"4094-3460-2754"[0:1] == "40"
"4094-3460-2754"[1:2] == "09"

因此,您可以访问多个字符并将它们视为整数:

int("4094-3460-2754"[0:1]) == 40

现在你可以把它们加起来,看看它们是否等于100。在

对于规则3,需要对所有数字求和,并检查除以4的余数是否为零:

  #condition 3
  s = 0
  for i in number:
    if i != '-':
      s += int(i)
  if s % 4 != 0:
    return "violates rule #3"

对于规则4,可以得到子字符串的int的和:

^{pr2}$

完整代码:

def verify(number) : # do not change this line!

  # write your code here so that it verifies the card number
  #condtion 1  
  if number[0] != '4':
    return "violates rule #1"

  #condition 2
  if int(number[3]) != (int(number[5]) + 1) :
    return  "violates rule #2"

  #condition 3
  s = 0
  for i in number:
    if i != '-':
      s += int(i)
  if s % 4 != 0:
    return "violates rule #3"

  if (int(number[0:2]) + int(number[7:9])) != 100:
    return "violates rule #4"

  # be sure to indent your code!
  return True # modify this line as needed

input = "4037-6000-0000" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!

我更喜欢先从数字中删除破折号-,这样可以很容易地处理。你也可以不用像你试过的那样去掉它。在

# split it into parts separated by dashes
# consider 4094-3460-2754
no_dashes = number.split('-')

print(no_dashes) # ['4094', '3460', '2754']

# combine the numbers without dashes
no_dashes = ''.join(no_dashes)

print(no_dashes) # 409434602754

# convert it into a list of integers so that it is more easier to work with
number = [int(x) for x in no_dashes]

print(number) # [4, 0, 9, 4, 3, 4, 6, 0, 2, 7, 5, 4]

您可以阅读split()join()here。在

现在,正如您所提到的,第一个条件很简单,您可以简单地检查第一个数字是否为4。在

^{pr2}$

第二个条件也很简单:

# 2nd condition
# 4th digit is a[3] and 5th digit is a[4]
if number[3] != number[4] + 1:
    return 'Viloates #2'

对于第三个条件,你只需要找到数字中每个数字的和。由于我们已经将数字转换为整数数组,因此使用sum()函数也很容易:

# 3rd condition
# Find the sum
num_sum = sum(number)

print(num_sum) # 48

# now check if the sum is divisible by 4
if num_sum % 4 != 0:
    return 'Violates #3'

现在,对于第四个条件,您需要将第一个和第二个数字视为两个数字,并与第七个和第八个数字相同。您可以将其转换为两位数字,如下所示:

# 1st digit is number[0]
# 2nd digit is number[1]
# 7th digit is number[6]
# 8ty digit is number [7]

# convert 1st two digits into a two-digit number
x = number[0] * 10 + number[1]

# convert 7th and 8th digits into a two-digit number
y = number[6] * 10 + number[7]

现在您可以检查它们的总和是否为100:

 if x + y != 100:
     return 'Violates #4'

因此,合并后的程序变为(合并了一些步骤):

def verify(number):
    number = [int(x) for x in ''.join(number.split('-'))]

    if number[0] != 4:
        return 'Violates #1'

    if number[3] != number[4] + 1:
        return 'Viloates #2'

    if sum(number) % 4 != 0:
        return 'Violates #3'

    if (number[0] * 10 + number[1] + number[6] * 10 + number[7]) != 100:
        return 'Violates #4'

    return True

但是上面的程序只会给出第一个失败的条件。如果需要,可以进一步修改,如下所示:

def verify(number):
    failed = []
    number = [int(x) for x in ''.join(number.split('-'))]

    if number[0] != 4:
        failed += [1]

    if number[3] != number[4] + 1:
        failed += [2]

    if sum(number) % 4 != 0:
        failed += [3]

    if (number[0] * 10 + number[1] + number[6] * 10 + number[7]) != 100:
        failed += [4]

    res = 'Violates ' + (', '.join[str(x) for x in failed])

    return res if len(failed) != 0 else 'Passed'

print(verify('4094-3460-2754')) # Passed
print(verify('4037-6000-0000')) # Violates 4

您可以再次修改它来显示通过的条件。我把它留给你!在

相关问题 更多 >