[英]Increase the first 15 digits of a 16 digit number by 1 within a range
Background背景
I am trying to ensure I am generating a unique credit card number
.我正在尝试确保生成一个唯一的
credit card number
。 The credit card number
needs to be 16 digits long with the last digit equal to the checksum
which in this case is self.checksum = 1
. credit card number
必须为16 位数字,最后一位数字等于checksum
,在本例中为self.checksum = 1
。
The first 6 digits of the credit card number
must be 400000
. credit card number
的前 6 位数字必须是400000
。
Since the last digit must be equal to the checksum
or 1
in this case, I believe I need to implement a range in my code somehow to indicate when the maximum credit card number has been issued.
由于在这种情况下,最后一位数字必须等于
checksum
或1
,我相信我需要在我的代码中以某种方式实现一个范围,以指示何时maximum credit card number has been issued.
In this case, the maximum credit card number
is 40000009999999991
.在这种情况下,最大
credit card number
为40000009999999991
。 Anything after that would change the first 6 digits.之后的任何内容都会更改前 6 位数字。
While the current solution "works" it does so by only adding 10
to the first possible credit card number
initialized in the __init__
as self.credit_card_number = 4000000000000001
.虽然当前的解决方案“有效”,但它只需将
10
添加到在__init__
中初始化为self.credit_card_number = 4000000000000001
的第一个可能的credit card number
上。
Help needed需要帮助
I am looking for help taking my existing code and implementing a range of some sort that can alert when the last credit card number
in the range has been issued.我正在寻求帮助,以获取我现有的代码并实施某种范围,当该范围内的最后一个
credit card number
已发出时,可以发出警报。
from random import randrange
class Accounts:
def __init__(self):
self.accounts_list = []
self.all_accounts = dict()
self.balance = 0
# Initial credit card number
self.credit_card_number = 4000000000000001
# Checksum is the last digit (16th) in the credit card number.
self.checksum = 1
# Pin number is generated in account_creation
self.pin = None
def main_menu(self):
while True:
main_menu_choice = input('1. Create an account\n'
'2. Log into account\n'
'0. Exit\n')
if main_menu_choice == '1':
self.account_creation()
def account_creation(self):
# Create credit card number ensuring it is unique by adding 1 to initialized value.
if len(self.accounts_list) == 0:
self.credit_card_number = self.credit_card_number
else:
self.credit_card_number = self.credit_card_number
self.credit_card_number = self.credit_card_number + 10
# Create pin number.
pin = int(format(randrange(0000, 9999), '04d'))
# Add credit card number to list used in the above if statement.
self.accounts_list.append(self.credit_card_number)
# Add the credit card number, pin, and balance to dictionary.
self.all_accounts[self.credit_card_number] = {'pin': pin, 'balance': self.balance}
# Print the output to make sure everything is OK.
print(self.accounts_list)
# Print the output to make sure everything is OK.
print(self.all_accounts)
print(f'\n'
f'Your card has been created\n'
f'Your card number:\n'
f'{self.credit_card_number}\n'
f'Your card PIN:\n'
f'{pin}'
f'\n')
Accounts().main_menu()
Can you update your init to generate credit cards:你能更新你的init来生成信用卡吗:
def __init__(self):
# do you stuff
self.credit_card_first_6 = '400000'
self.checksum = '1'
# this will be used to create a unique credit card
self.count = 0
# middle numbers MUST be smaller than this
self.max_count = 1000000000
def account_creation(self):
#### do your stuff
# for this user they have a unique 9 digits in the middle
# this is then zero padded using zfill
unique_id = str(self.count).zfill(9)
# create the full credit card number
# note: store each bit as a str so we can concat then convert to int
credit_card_number = int(self.credit_card_first_6 + unique_id + checksum)
self.count += 1
# update your init values when your limit is reached
if self.count >= self.max_count:
self.count = 0
self.credit_card_first_6 = str(int(self.credit_card_first_6) + 1)
Since you marked Matt's answer as a valid I've added a quick refactor with some extra code that might be use full to you.由于您将马特的答案标记为有效,我添加了一个快速重构,其中包含一些可能对您完全有用的额外代码。
class Account:
balance = 0
__pin = None # this are
__number = None # private members
def __eq__(self, other):
return self.__number == other
def is_pin(self, pin):
return self.__pin == pin
def number(self):
return self.__number
# adding a 'is not none' makes
# it a 'write only once' variable,
# if a 'none' text is added as a input
# text is added not a None type
def set_pin(self, pin):
if self.__pin is None:
self.__pin = pin
return True
return False
def set_number(self, num):
if self.__number is None \
and len(str(num)) == 16:
self.__number = num
return True
return False
# eeextra simple checksum
def checksum(self):
ck_sum = 0
for i in str(self.__number):
ck_sum += int(i)
return int(str(ck_sum)[-1])
class Accounts:
base_num = 4000000000000000
def __init__(self):
self.accounts = []
self.num_offset = 0
@staticmethod
def dialog_choice():
choice = input(
'1. Create an account\n'
'2. Log into account\n'
'0. Exit \n \n'
)
return choice
def next_card_num(self):
self.num_offset += 1
return self.base_num + self.num_offset
def dialog_acount_creation(self):
card_pin = input('New pin ->')
card_num = self.next_card_num()
print('Card number ->', card_num)
return card_num, card_pin
@staticmethod
def dialog_login():
card_num = input('Card number ->')
card_pin = input('Card pin ->')
return int(card_num), card_pin
@staticmethod
def dialog_error(*args):
print('Error on acount', args[0])
def main_loop(self):
dia_map = {
'1': self.dialog_acount_creation,
'2': self.dialog_login,
}
cho_map = {
'1': self.account_creation,
'2': self.account_login,
}
err_map = {
'1': self.dialog_error,
'2': self.dialog_error,
}
while True:
o = self.dialog_choice()
if o == '0':
break
if o in cho_map:
q_dialog = dia_map[o]()
q_done = cho_map[o](*q_dialog)
if not q_done:
err_map[o](*q_dialog)
else:
print('Invalid option')
def account_login(self, num, pin):
for acc in self.accounts:
if acc == num and acc.is_pin(pin):
print('You are logged in !')
return True
return False
def account_creation(self, num, pin):
new_accaunt = Account()
new_accaunt.set_number(num)
new_accaunt.set_pin(pin)
self.accounts.append(new_accaunt)
return True
if __name__ == '__main__':
h_acc = Accounts()
h_acc.account_creation(4000000000000000, '1234')
h_acc.main_loop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.