2016-09-28 35 views
-4

我想使這個程序充當銀行,我如何確保正確的ID號碼必須輸入正確的PIN碼,並根據您輸入的ID輸入print hello然後輸入他們的名字和提示他們在銀行有多少錢。銀行ATM程序登錄

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

很好!你有特定的問題或問題嗎? – MooingRawr

+0

您需要鏈接id-name-balance-pin,所以更好的方法是創建一個類並將其實例存儲在列表中。 –

回答

0

我對你在程序中闡述了多少印象深刻。以下是我將如何查看您的解決方案。


所以要創建一個登錄模擬,我會改用字典。這樣你可以分配一個ID到一個PIN。例如:

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

如果你的ID是對結腸的左側和密碼是正確的。你還必須使用,你猜對了一個字典,把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以及最後2行的while循環。 希望我幫忙。乾杯!

+0

好吧,讓我明白你已經做了什麼,但如果用戶正在創建一個銀行賬戶,那麼如何將你的東西添加到字典? –

+0

像這樣:credentials [「insertnumberhere」] =「insertothernumber」 – Mangohero1

+0

我也忘了提及我的代碼是爲python 2編寫的,所以如果你使用的是python 3,唯一需要改變的就是登錄,'print「Hello ,{}!「。格式(bankIDs [loginid])' – Mangohero1