简体   繁体   English

银行 ATM 程序登录

[英]Bank ATM Program login

I want to make this program that acts as a bank, how do I make sure the correct ID number must be entered with the correct pin and have it depending on the id you entered print hello then their name and prompt how much money they have in the bank.我想制作这个充当银行的程序,我如何确保必须使用正确的密码输入正确的 ID 号,并根据您输入的 ID 输入正确的 ID 号,然后打印他们的姓名并提示他们有多少钱银行。

attempts = 0
store_id = [1057, 2736, 4659, 5691, 1234, 4321]
store_name = ["Jeremy Clarkson", "Suzanne Perry", "Vicki Butler-Henderson", "Jason Plato"]
store_balance = [172.16, 15.62, 23.91,  62.17, 131.90, 231.58]
store_pin = [1057, 2736, 4659, 5691]

start = int(input("Are you a member of the Northern Frock Bank?\n1. Yes\n2. No\n"))
if start == 1:
    idguess = ""
    pinguess = ""
    while (idguess not in store_id) or (pinguess not in store_pin):
        idguess = int(input("ID Number: "))
        pinguess = int(input("PIN Number: "))
        if (idguess not in store_id) or (pinguess not in store_pin):
            print("Invalid Login")
           attempts = attempts + 1
        if attempts == 3:
            print("This ATM has been blocked for too many failed attempts.")
            break

elif start == 2:
    name = str(input("What is your full name?: "))
    pin = str(input("Please choose a 4 digit pin number for your bank account: "))
    digits = len(pin)
    balance = 100

while digits != 4:
    print("That Pin is Invalid")
    pin = str(input("Please choose a 4 digit pin number for your bank account: "))
    digits = len(pin)

store_name.append(name)
store_pin.append(pin)

I'm very impressed by how much you've elaborated on your program.您对程序的详细阐述给我留下了深刻的印象。 Here's how I would view your solution.这是我如何看待您的解决方案。


So to create a login simulation, I would instead use a dictionary.因此,要创建登录模拟,我会改用字典。 That way you can assign an ID to a PIN.这样您就可以为 PIN 分配一个 ID。 For example:例如:

credentials = {
    "403703": "121",
    "3900": "333",
    "39022": "900"
}

Where your ID is on the left side of the colon and the PIN is on the right.您的 ID 位于冒号左侧,PIN 位于右侧。 You would also have to assign the ID to a name that belongs to that ID using, you guessed it, a dictionary!您还必须使用字典将 ID 分配给属于该 ID 的名称!

bankIDs = {
    "403703": "Anna",
    "3900": "Jacob",
    "39022": "Kendrick"
}

Now that you've done that, you can create your virtual login system using if/else control flow.现在您已经完成了,您可以使用if/else控制流创建您的虚拟登录系统。 I've made my code like this:我已经让我的代码是这样的:

attempts = 0
try:
    while attempts < 3:
        id_num = raw_input("Enter your ID: ")
        PIN = raw_input("Password: ")
        if (id_num in credentials) and (PIN == credentials[id_num]):
            print "login success."
            login(id_num)
        else:
            print "Login fail. try again."
            attempts += 1
    if attempts == 3:
        print "You have reached the maximum amount of tries."
except KeyboardInterrupt:
    print "Now closing. Goodbye!"

Note the try and except block is really optional.请注意 try 和 except 块实际上是可选的。 You could use the break operator like you did in your code if you wanted to, instead.如果愿意,您可以像在代码中那样使用break运算符。 I just like to put a little customization in there (Remember to break out of your program is CTRL-C).我只是想在那里进行一些自定义(记住要打破你的程序是 CTRL-C)。 Finally, Python has a way of making life easier for people by using functions .最后,Python 有一种方法可以通过使用函数使人们的生活更轻松。 Notice I used one where I put login(id_num) .请注意,我在放置login(id_num)地方使用了一个。 Above this while loop you'll want to define your login so that you can display a greeting message for that particular person.在此 while 循环之上,您需要定义您的登录名,以便您可以显示该特定人员的问候消息。 Here's what I did:这是我所做的:

def login(loginid):
    print "Hello, %s!" % bankIDs[loginid]

Simple use of string formatting.字符串格式的简单使用。 And there you have it.你有它。 The same can be done with displaying that person's balance.显示该人的余额也可以这样做。 Just make the dictionary for it, then print the code in your login definition.只需为它制作字典,然后在您的登录定义中打印代码。 The rest of the code is good as it is.其余的代码是好的。 Just make sure you've indented properly your while-loop inside the elif on the bottom of your code, and your last 2 lines as well.只需确保您在代码底部的elif 中正确缩进了 while 循环,以及最后 2 行。 Hope I helped.希望我有所帮助。 Cheers!干杯!

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

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