如何在字符串中加数字

时间:2019-08-26 16:58:17

标签: python

我是python的新手,我做作业来验证信用卡号。我完成了前两个条件,但我坚持使用#3和#4条件。感谢您的帮助

条件:

  1. 第一位数字必须为4.-完成
  2. 第四位必须比第五位大一位;请记住,由于格式为####-####-##### .- done
  3. ,所以它们之间用短划线隔开
  4. 所有数字的总和必须能被4-需要帮助平均除
  5. 如果您将前两位数字视为两位数字,并将第七位和第八位视为 作为两位数,它们的总和必须为100。-需要帮助
@watchdog
async def func2(self):

预期输出:

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!

4 个答案:

答案 0 :(得分:0)

对于规则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之和:

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

完整代码:

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!

答案 1 :(得分:0)

对于条件#3

Python中的字符串是可迭代的,这意味着您可以在for循环中传递它们。循环中的每个元素都是字符串中的一个字符。因此,如果您这样做

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

您会得到:

4
0
9
4
-
3
4
etc.

使用此方法,您可以计算输入中每​​个数字的总和,并检查是否可以被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中的字符串还可以通过其索引(从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。

答案 2 :(得分:0)

  

所有数字的总和必须被4整除。

您可以使用以下条件语句进行检查:

if sum_of_nums % 4 != 0:
   print("Violates rule 3!")

这将检查除以4时是否有余数,如果没有余数,它将平均除并且表达式等于零。如果不均分,它将不等于0!

  

如果您将前两位数字视为两位数字,并将第七位和第八位数字视为两位数字,则它们的总和必须为100。

在这里您可以像引用列表一样引用字符串的字符。如果输入始终保持一致,则可以对引用进行硬编码,将它们更改为int,然后将它们加在一起并使用条件语句进行检查

first_num = input[0]+input[1]
first_num = int(first_num) #now an int

second_num = input[6]+input[7]
second_num = int(second_num)

if first_num + second_num != 100:
   print("Violates rule 4!")

答案 3 :(得分:0)

我更喜欢先从数字中删除破折号-,以便于使用。您也可以像不尝试一样将其删除。

# 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。

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

第二个条件也很简单:

# 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'

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

# 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

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