Relevant Background:
Greetings. This is my first attempt at using the OOP paradigm (only really used R
prior to this).
The project prompt came from JetBrains Academy
where I am currently working through the Python Developer
track. The code works as expected and was accepted for the particular stage I am on, but I have some specific questions that will hopefully improve the program.
Objective:
You should allow customers to create a new account in our banking system.
Program Details:
Once the program starts, you should print the menu:
1. Create an account
2. Log into account
0. Exit
In our banking system, the credit card number's IIN (first 6 digits) must be 400000.
We often see 16-digit credit card numbers today, but it’s possible to issue a card with up to 19 digits using the current system. In the future, we may see longer numbers becoming more common. In our banking system, the customer account number can be any, but it should be unique. And the whole card number should be 16-digit length.
If the customer chooses ‘Create an account’, you should generate a new card number which satisfies all the conditions described above. Then you should generate a PIN code that belongs to the generated card number. A PIN code is a sequence of any 4 digits. PIN should be generated in a range from 0000 to 9999.
If the customer chooses ‘Log into account’, you should ask them to enter their card information. Your program should store all generated data until it is terminated so that a user is able to log into any of the created accounts by a card number and its pin. You can use an array to store the information.
After all information is entered correctly, you should allow the user to check the account balance; right after creating the account, the balance should be 0. It should also be possible to log out of the account and exit the program.
Specific Questions:
I'd like to know how to have the code chunk below print just
Bye!
and notBye!
and thenNone
below it:elif account_balance_selection == "0": print("\n") print("Bye!") > Bye! > None should be: > Bye!
I am using the following code to generate and store the
credit card number
andpin number
.def create_account(self): credit_card_number = "400000" + format(randint(0000000000, 9999999999), '010d') pin_number = format(randint(0000, 9999), '04d') self.card_numbers.append(credit_card_number) self.pin_numbers.append(pin_number)
Is there a way to ensure that the numbers that are getting added to card_numbers
and pin_numbers
are unique? Either during the generation stage, or somehow prior to appending them to the their respective arrays?
Code:
from random import randint
class BankingSystem:
def __init__(self):
self.card_numbers = []
self.pin_numbers = []
self.iin = 400000
def main_welcom_screen(self):
print(
"1. Create an account\n"
"2. Log into account\n"
"0. Exit"
)
main_menu_selection = str(input())
if main_menu_selection == "1":
self.create_account()
if main_menu_selection == "2":
self.account_login()
if main_menu_selection == "0":
print("\n")
return "Bye!"
def create_account(self):
credit_card_number = "400000" + format(randint(0000000000, 9999999999), '010d')
pin_number = format(randint(0000, 9999), '04d')
self.card_numbers.append(credit_card_number)
self.pin_numbers.append(pin_number)
print("\n")
print("Your card has been created")
print("Your card number:")
print(credit_card_number)
print("Your pin number:")
print(pin_number)
print("\n")
self.main_welcom_screen()
def account_login(self):
print("\n")
print("Enter your card number:")
entered_card_number = int(input())
print("Enter your PIN:")
entered_pin_number = int(input())
if str(entered_card_number) in self.card_numbers and str(entered_pin_number) in
self.pin_numbers:
print("\n")
print("You have successfully logged in!")
self.account_balance()
else:
print("\n")
print("Wrong card number of PIN!")
print("\n")
self.main_welcom_screen()
def account_balance(self):
print(
"\n1. Balance\n"
"2. Log out\n"
"0. Exit"
)
account_balance_selection = str(input())
if account_balance_selection == "1":
print("\n")
print("Balance: 0")
self.account_balance()
elif account_balance_selection == "2":
print("\n")
print("You have successfully logged out!")
print("\n")
self.main_welcom_screen()
elif account_balance_selection == "0":
print("\n")
print("Bye!")
print(BankingSystem().main_welcom_screen())