银行ATM程序登录

时间:2016-09-28 13:31:46

标签: python

我想制作一个充当银行的程序,如何确保必须使用正确的PIN输入正确的ID号并根据您输入的ID打印hello然后他们的名字并提示多少钱他们在银行里。

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)

1 个答案:

答案 0 :(得分:0)

你对你的节目详细阐述了多少印象深刻。以下是我查看解决方案的方法。

因此,要创建登录模拟,我会使用字典。这样您就可以为PIN分配ID。例如:

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

您的ID位于冒号左侧,PIN位于右侧。您还必须使用(您猜对了)字典将ID分配给属于该ID的名称!

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

现在您已经完成了这项工作,您可以使用 if / else 控制流创建虚拟登录系统。我的代码是这样的:

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!"

请注意,try和except块实际上是可选的。如果您愿意,可以像在代码中一样使用break运算符。我只想在那里进行一些自定义(记住打破你的程序是CTRL-C)。 最后,Python有一种通过使用函数让人们的生活更轻松的方法。注意我在login(id_num)放置了一个。在此while循环之上,您将需要定义登录名,以便您可以为该特定人员显示问候语消息。这是我做的:

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

简单使用字符串格式。你有它。显示该人的余额也可以这样做。只需为它创建字典,然后在您的登录定义中打印代码。 其余的代码很好。只需确保您在代码底部的 elif 内部以及最后两行中正确缩进了while循环。 希望我帮忙。干杯!