-1

I'm writing a very basic payment system on Python, but every time I enter the main() function and input a value it just quits the program. Any ideas?

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()
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 1
    You need to tell us more ... what input do you provide, do you see anything after the first input or does it end right after the first input. also, independently of your choice, the program will end after `main()` ends. – Julien Spronck Jun 14 '17 at 18:34
  • 3
    a good way to start debugging a problem is to print the repr of the value you are testing (ie `print(repr(choice))` ) at a guess this is code for python3 that someone gave you or you found and altered to your needs, and you are running python 2 (even though you tagged this python3x) ... also maybe pur a print statemetn in your else branches to figure out where you are going wrong – Joran Beasley Jun 14 '17 at 18:34
  • 1
    read the error message, then look up function scoping – c2huc2hu Jun 14 '17 at 18:35
  • if you type `print(type(choice))` after the first line, what do you get? i'm guessing `` – Julien Spronck Jun 14 '17 at 18:37
  • Possible duplicate of [Why use def main()?](https://stackoverflow.com/questions/4041238/why-use-def-main) – Sam Hartman Jun 14 '17 at 18:40

3 Answers3

1

Since you have provided no other information and the code runs fine on my machine, I am going to assume that your error is that you are running the wrong version of python. The code compiles with but when you get to any of the inputs, it will not work as desired:

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:~ $ 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

I ran your code with python3.x and it has some errors.

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

You can subscript a function object. What you really need to do is either pass the correct username password to the main function so that it can be accessed in the main function to add balance and other things and validate the password again.

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
-1

First of all, your program works fine apart from the below code,

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

You are trying to access info as an array, but info is a function. (may be you have missed to define array for your payment system menu options).

Suneeth Lenin
  • 285
  • 1
  • 4
  • 10