简体   繁体   English

Python:银行ATM程序

[英]Python: Bank ATM program

In my Python program I have created one class userdetails - (although I have some other classes I want to add later).在我的 Python 程序中,我创建了一个 class 用户详细信息 - (尽管我还有一些其他类我想稍后添加)。 I have created 2 objects from that class but I cannot call all of them?我从那个 class 创建了 2 个对象,但我不能全部调用它们? this is my issue.这是我的问题。 I want to link seperate pin_no's to seperate objects and call them individually with their details.我想将单独的 pin_no 链接到单独的对象并使用它们的详细信息单独调用它们。


from Details import userdetails


pin_no = (1111, 2222)

while True:

    pin_no = input("Input the no : ")

    if pin_no == '1111' or pin_no == '2222':
        
        
        print ("\n Hello and welcome to my program.  Please choose from one of the following options:")
        
        break
    
    else:
    
        print ("please try again ")
            
            
            
newuserdetails = userdetails ('Tom', '23 Bishop St ', 'cv3_f45', 2347477472)

newuserdetails1 = userdetails ('Bill', '81 Oliver St ', 'CV6 7FR', 574747477)   
  
  
user =  input("\n\n 1. Userdetails \n 2. Address \n 3. Post Code \n 4. Tel No " '\n')

a = '1'
b = '2'
c = '3'
d = '4'


if user == a:

    print (newuserdetails.name) or (newuserdetails1.name) 
    
elif user == b:
    
    print (newuserdetails.address)
     
elif user == c:
    
    print (newuserdetails.post_code)
    
elif user == d:
    
    print (newuserdetails.tel_no)

I believe what are looking for is a dictionary.我相信正在寻找的是一本字典。

newuserdetails = userdetails ('Tom', '23 Bishop St ', 'cv3_f45', 2347477472)
newuserdetails1 = userdetails ('Bill', '81 Oliver St ', 'CV6 7FR', 574747477)
newuserdetails2 = userdetails ('John', '35 Main St ', 'CRF 250R', 435247477)


# create the dictionary right after you create the class, so they can be in the same scope
# whenever you would reference newuserdetails, you can now reference the dictionary object

dict = { # if you know all the users when you create the dictionary
  '1111': newuserdetails,
  '2222': newuserdetails1,
}

# to add a user after the dictionary has been created
dict['3333'] = newuserdetails2

Then you can get the user details instance by the pin, using this:然后,您可以通过 pin 获取用户详细信息实例,使用以下方法:

dict['1111'] # returns newuserdetails

# instead of using:
newuserdetails.name # returns 'Tom'

# you can now use:
dict['1111'].name # returns 'Tom'

# Or you could assign it to a temp variable if that makes more sense
currentuserdetails = dict['1111']
currentuserdetails.name # still returns 'Tom'

I warn against using a pin as a way to get the user, however.但是,我警告不要使用 pin 作为获取用户的方式。

If two users choose the same pin, by chance, then there will be errors and your code will break.如果两个用户偶然选择了相同的 pin,那么就会出现错误并且您的代码会中断。 You would be better suited to use Ids to get the userdetails您会更适合使用 Ids 来获取用户详细信息

Edit:编辑:

Here is what I'm using and it works as expected on python 3.8这是我正在使用的,它在 python 3.8 上按预期工作

class userdetails:
    def __init__(self, name, addr, num, number):
        self.name = name
        self.address = addr
        self.post_code = num
        self.tel_no = number


pin_no = (1111, 2222)

while True:
    pin_no = input("Input the no : ")
    if pin_no == '1111' or pin_no == '2222':
        print ("\n Hello and welcome to my program.  Please choose from one of the following options:")     
        break
    
    else:
        print ("please try again ")
            
               
newuserdetails = userdetails ('Tom', '23 Bishop St ', 'cv3_f45', 2347477472)
newuserdetails1 = userdetails ('Bill', '81 Oliver St ', 'CV6 7FR', 574747477)   
dict = {
  '1111': newuserdetails,
  '2222': newuserdetails1
}

user =  input("\n\n 1. Userdetails \n 2. Address \n 3. Post Code \n 4. Tel No " '\n')

a = '1'
b = '2'
c = '3'
d = '4'


if user == a:
    print (dict[pin_no].name)
elif user == b:
    print (dict[pin_no].address)
elif user == c:
    print (dict[pin_no].post_code)
elif user == d:
    print (dict[pin_no].tel_no)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM