在python中使用循环来配置信用卡号
我正在做这个项目,我必须检查信用卡号码是否有效.在这种情况下,我只需要一张 8 位数的信用卡(我知道这不是现实).问题来了
I am working on this project in which I have to check if a credit card number is valid or not. In this case I only need an 8 digit credit card(I know that's not reality). Here is the question
信用卡号的最后一位是校验位,可防止出现转录错误,例如一位数错误或两位数转换.以下方法用于验证实际信用卡号码,但为简单起见,我们将使用 8 位而不是 16 位数字来描述它:
The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:
• 从最右边的数字开始,形成每个其他数字的总和数字.例如,如果信用卡号是 4358 9795
,那么您形成总和 5 + 7 + 8 + 3 = 23.
• Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is
4358 9795
, then you form the sum5 + 7 + 8 + 3 = 23.
• 将前面未包含的每个数字加倍步.将结果数字的所有数字相加.例如,与上面给出的数字,将数字加倍,从倒数第二个,产生 18 18 10 8
.添加这些值中的所有数字产生 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27
.
• Double each of the digits that were not included in the preceding
step. Add all digits of the resulting numbers. For example, with the
number given above, doubling the digits, starting with the
next-to-last one, yields 18 18 10 8
. Adding all digits in these values
yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27
.
• 将前面两个步骤的总和相加.如果最后一位数结果为0,数字有效.在我们的例子中,23 + 27 = 50,所以号码有效.
• Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid.
编写一个程序来实现这个算法.用户应提供一个 8 位数字,并打印出该数字是否有效.如果它无效,您应该打印使其有效的校验位值.
Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print the value of the check digit that would make it valid.
我必须使用循环来计算总和.但是,我不知道如何使用循环.
I have to use loops to do the sum. However, I do not know how to use loops for that.
这是我的代码
# Credit Card Number Check. The last digit of a credit card number is the check digit,
# which protects against transcription errors such as an error in a single digit or
# switching two digits. The following method is used to verify actual credit card
# numbers but, for simplicity, we will describe it for numbers with 8 digits instead
# of 16:
# Starting from the rightmost digit, form the sum of every other digit. For
# example, if the credit card number is 43589795, then you form the sum
# 5 + 7 + 8 + 3 = 23.
# Double each of the digits that were not included in the preceding step. Add # all
# digits of the resulting numbers. For example, with the number given above,
# doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding
# all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
# Add the sums of the two preceding steps. If the last digit of the result is 0, the
# number is valid. In our case, 23 + 27 = 50, so the number is valid.
# Write a program that implements this algorithm. The user should supply an 8-digit
# number, and you should print out whether the number is valid or not. If it is not
# valid, you should print out the value of the check digit that would make the number
# valid.
card_number = int(input("8-digit credit card number: "))
rights = 0
for i in card_number[1::2]:
rights += int(i)
lefts = 0
for i in card_number[::2]:
lefts += int(i)*2%10+int(i)*2/10
print card_number, (rights +lefts)/10
if remaining == 0:
print("Card number is valid")
else:
print("Card number is invalid")
if digit_7 - remaining < 0:
checkDigit = int(digit_7 + (10 - remaining))
print("Check digit should have been:", checkDigit)
else:
checkDigit = int(digit_7 - remaining)
print("Check digit should have been:", checkDigit)
这是另一种可能的解决方案:
Here is another possible solution:
cc_number = input("8-digit credit card number: ").replace(" ", "")
total1 = 0
total2 = 0
for digit in cc_number[-1::-2]:
total1 += int(digit)
for digit in cc_number[-2::-2]:
total2 += sum(int(x) for x in str(int(digit)*2))
remainder = (total1 + total2) % 10
if remainder == 0:
print("Card is valid!")
else:
invalid_check_digit = int(cc_number[-1])
if invalid_check_digit - remainder < 0:
valid_check_digit = invalid_check_digit + (10 - remainder)
else:
valid_check_digit = invalid_check_digit - remainder
print("Card is invalid - the correct check digit is {}".format(valid_check_digit))
为您提供以下输出:
8-digit credit card number: 4358 9795
Card is valid!
或者:
8-digit credit card number: 4358 9794
Card is invalid - the correct check digit is 5