2017-06-14 65 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) 

您试图访问信息作为数组,但信息是一个函数。 (可能是您错过了为您的支付系统菜单选项定义数组)。