功能将无法继续

2024-06-12 10:07:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: toinfoinputyourindexifmainsys
3条回答

由于您没有提供其他信息,并且代码在我的机器上运行良好,因此我将假设您的错误是运行了错误的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:~ $ 

首先,除了下面的代码,你的程序运行得很好

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

您试图以数组的形式访问info,但info是一个函数。(可能是您没有为您的支付系统菜单选项定义数组)。你知道吗

我用python3.x运行了您的代码,它有一些错误。你知道吗

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

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

相关问题 更多 >