2017-06-14 165 views
-1

我正在Python上編寫一個非常基本的支付系統,但每次輸入main()函數並輸入一個值時,它都會退出程序。有任何想法嗎?函數不會繼續

import sys 


def info(): 
    usernames = ["15mermelstein_p","gril","jeff","example"] 
    passwords = ["test","pass","what","why"] 
    balances = [22.91,45.76,21.62,7.54] 
    a = input("What is your username? ").lower() 
    b = input("What is your password? ") 
    print(a) 
    print(b) 
    success = 0 
    for index in range(len(usernames)): 
     if usernames[index] == a and passwords[index] == b: 
      user_num = index 
      success = 1 
      break 
    if success == 1: 
     print("Login successful\n") 
     main() 
     return (a,b,user_num) 
    else: 
     print("Login unsuccessful") 
     info() 

def main(): 
    choice = input("-----Welcome to the new School Payment System-----\n1. Add money to account\n2. Change password\n3. Change your username\n4. Quit the program\n--------------------------------------------------\n") 
    if choice == "1": 
     credit = input("How much money would you like to deposit into your account? ") 
     temp_b = input("Please type in your password once againto confirm thios transaction: ") 
     if temp_b == info[2]: 
      balances[num(info[3])] += float(credit) 
     else: 
      print("The password you entered was incorrect, please return and try again. ") 
    elif choice == "2": 
     check_pass = input("Please input your current password first") 
    elif choice == "3": 
     sys.exit(0) 
    elif choice == "4": 
     sys.exit(0) 
    else: 
     sys.exit(0) 



info() 
+1

您需要告訴我們更多...您提供什麼輸入,您是否在第一次輸入後看到任何內容,或者是否在第一次輸入後立即結束。另外,獨立於您的選擇,程序將在'main()'結束後結束。 –

+3

開始調試問題的一個好方法是打印出您正在測試的值的repr(例如,'print(repr(choice))'),這是有人給您發現的或者您發現並修改爲python3的代碼你的需要,你正在運行Python 2(即使你標記了這個python3x)...也許在你的其他分支中找出一個print statemetn來找出你要出錯的地方 –

+1

讀取錯誤信息,然後查找函數範圍 – user3080953

回答

1

既然你沒有提供其他信息和代碼運行在我的機器上很好,我會假設你的錯誤是,你正在運行的Python的版本錯誤。代碼編譯與但是當你到任何輸入,它不會如期望的那樣工作:

AJs-MacBook-Pro:~ $ python2 k.py 
What is your username? "gril" 
What is your password? "pass" 
gril 
pass 
Login successful 

-----Welcome to the new School Payment System----- 
1. Add money to account 
2. Change password 
3. Change your username 
4. Quit the program 
-------------------------------------------------- 
1 
AJs-MacBook-Pro:~ $ 
0

python3.x跑了你的代碼,它有一些錯誤。

if temp_b == info[2]: 
    balances[num(info[3])] += float(credit) 

您可以下標函數對象。您真正需要做的是將正確的用戶名密碼傳遞給主函數,以便可以在主函數中訪問它以添加餘額和其他內容並再次驗證密碼。

-1

首先,你的程序工作正常,除了下面的代碼,

if temp_b == info[2]: 
    balances[num(info[3])] += float(credit) 

您試圖訪問信息作爲數組,但信息是一個函數。 (可能是您錯過了爲您的支付系統菜單選項定義數組)。