Say i have a class “Bank” which instances hold Customer objects in dictionaries keyed by their customer_id (or something like that).
Customer in turn has attributes “Name” and “personalId/ Social Security number/ stateId”
Two customers have to be able to share the same name (Happens sometime in real life if you have a common first and last name), but they should not be able to have the same personalIdNbr since that implies we have registered the same customer multiple times as different people.
def newCustomer(bank_: Bank) -> None:
try:
name = input("Name: ")
personIdNbr = input("Social security number: ")
customerId = bank_.add_customer(name, personIdNbr) #raise here?
print(f"Customer added with customerid: {customerId}")
except MyException:
print(f"Customer with social security {personIdNbr} allready in system")
except KeyboardInterupt:
print("No new customer added.")
finally
input() # Give them a chance to read whats printed before the program continues
Also:
would it be “pythonic” to create my own exception for this usecase? What should i name it then?
so far add_customer()
simply returns ‘None’ when the social security is allready registered (thats simply part of the spec), and I have an if block to check if this is the case, but it doesn’t seem like a very “pythonic” way to do things, (more LYBL than EAFP).
Minimo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.