背景
我试图确保自己产生的是唯一的credit card number
。的credit card number
需要是16位长与最后一个数字等于checksum
在此情况下是self.checksum = 1
。
的前6位数字credit card number
必须为400000
。
由于最后一个数字必须等于checksum
或1
在这种情况下,因此我认为我需要以某种方式在代码中实现一个范围,以指示何时,maximum credit card number has been issued.
在这种情况下,最大值credit card number
为40000009999999991
。之后的所有内容都会更改前6位数字。
当前解决方案“有效”时,它仅通过10
将第一个可能的credit card number
初始化添加到__init__
as中来实现self.credit_card_number = 4000000000000001
。
需要帮助
我正在寻求帮助,以获取现有代码并实现某种范围,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()
您能否更新您的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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句