功能不会继续

时间:2017-06-14 18:29:05

标签: python python-3.x function input

我在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()

3 个答案:

答案 0 :(得分: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:~ $ 

答案 1 :(得分:0)

我使用python3.x运行了您的代码,但它有一些错误。

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

您可以下标函数对象。您真正需要做的是将正确的用户名密码传递给main函数,以便可以在main函数中访问它以添加余额和其他内容并再次验证密码。

答案 2 :(得分:-1)

首先,除了以下代码之外,您的程序运行正常,

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

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