所以今天我有一个任务,要求我构建一个银行账户类。我拥有一切以使我的输出与预期的输出相匹配。
我的输出:
Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $20000
Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $15000
Alin,Smith (SSN:111-11-1111) account number: 5040470401, balance: $15100.0
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10000
Mary,Lee (SSN:222-22-2222) Insufficient Funds to withdraw: $15000
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10000
Mary,Lee (SSN:222-22-2222) account number: 5040470401, balance: $10500.0
然而,正如您所看到的,即使 Mary 和 Alin 是不同的客户,他们也会生成相同的帐号。
我今天的问题是如何为在我的银行帐户类中创建的每个客户对象提供不同的随机 10 位帐号。
现在我有我的客户课程
class Customer:
def __init__(self,firstName, lastName, social):
self.firstName = firstName
self.lastName = lastName
self.social = social
def setfirstName(self,firstName):
self.firstName = firstName
def setlastName(self,lastName):
self.lastName = lastName
def __str__(self):
self.name = "{},{} (SSN:{})".format(self.firstName, self.lastName,self.social)
return self.name
然后是我的 BankAccount 类(仅包括随机数部分):
class BankAccount(Customer):
from random import randint
n = 10
range_start = 10**(n-1)
range_end = (10**n)-1
accountNumber = randint(range_start, range_end)
def __init__(self,customer,balance = 0):
self.customer = customer
self.balance = balance
def setCustomer(self,customer,accountNumber):
self.customer = customer
self.accountNumber = accountNumber
def getCustomer(self,customer,accountNumber):
return self.customer, self.accountNumber
def __str__(self):
customer = "{} account number: {}, balance: ${}".format(self.customer,self.accountNumber,self.balance)
return customer
我认为对于我创建的每个帐户对象,都会生成一个新的随机数,但似乎并非如此。
有任何想法吗?