【发布时间】:2021-02-15 17:28:26
【问题描述】:
背景
我正在努力确保生成一个唯一的credit card number。 credit card number 需要 16 位长,最后一位等于 checksum,在本例中为 self.checksum = 1。
credit card number 的前 6 位数字必须是 400000。
由于在这种情况下最后一位数字必须等于checksum 或1,我相信我需要在我的代码中以某种方式实现一个范围以指示maximum credit card number has been issued. 在这种情况下,最大@987654330 @ 是 40000009999999991。之后的任何内容都会更改前 6 位数字。
虽然当前的解决方案“有效”,但它只需将10 添加到__init__ 中初始化为self.credit_card_number = 4000000000000001 的第一个可能的credit card number。
需要帮助
我正在寻求帮助,以获取我现有的代码并实现某种范围,当该范围中的最后一个 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()
【问题讨论】: