0

Things I want to achieve:

1. Login
2. Reset Password
3. Quit
What would you like to do? 2
Student ID: afrank
Error! Please enter your Student ID Number.
Student ID: 39211111x
Error! Please enter your Student ID Number.
Student ID: 39211111111
Error! Student ID Not Found
Student ID: 392111111
User Name: afrank
What year were you admitted? 2018
New Password: abc456
Confirm New Password: abc456
Password Changed!
1. Login
2. Reset Password
3. Quit
What would you like to do? 3

Codes:

def reset_password():

    isUser  = False
    changed = False

    sid = input('Student ID: ')
    try:
        sid = int(sid)
        import math
        digits = int(math.log10(sid)) + 1
        if digits != 9:
            print('Error: Student ID Not Found')
            reset_password()
        else:
            pass
    except:
        print('Error: Please enter your Student ID Number.')
        reset_password()

def options_menu():

    print('1. Login'+'\n'+'2. Reset Password'+'\n'+'3. Quit'+'\n'+'What would you like to do?')
    option = int(input())

    if option == 1:
        hello_login()
    elif option == 2:
        reset_password()
    elif option == 3:
        pass
options_menu()

Outputs:

1. Login
2. Reset Password
3. Quit
What would you like to do? 2
Student ID: afrank
Error! Please enter your Student ID Number.
Student ID: 39211111x
Error! Please enter your Student ID Number.
Student ID: 39211111111
Error! Student ID Not Found
Student ID: 392111111
User Name: afrank
What year were you admitted? 2018
New Password: abc456
Confirm New Password: abc456
Password Changed!
1. Login
2. Reset Password
3. Quit
What would you like to do? 3
Error! Please enter your Student ID Number
Error! Please enter your Student ID Number
Error! Please enter your Student ID Number
Process finished with exit code 0

I'm new to Python. I noticed the error repeated 3 times, which part did I do wrong in the recursive function?

5
  • Don't use recursion for looping. Commented Nov 19, 2019 at 4:26
  • Do you mean use while loop?@Barmar Commented Nov 19, 2019 at 4:27
  • yes, that's what I mean. Also, I don't see where your code asks for the year and new password. Commented Nov 19, 2019 at 4:29
  • @Barmar, this is part of the function codes. I think this is the part where I made a mistake. I didn't use recursive functions in the rest of the codes. So I didn't paste them here. Could you show me a while loop example based on my codes? Thank you! Commented Nov 19, 2019 at 4:32
  • You'll learn better if you try to write it yourself. Programming is not paint-by-numbers. Commented Nov 19, 2019 at 4:36

1 Answer 1

2

Do not use such function calls to restart your function, recursion is not used in such a situation.

A recursive function has to fulfill an important condition to be used in a program: it has to terminate. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case.

If you want to restart the reset_password function, simply use a while loop to wrap in the input validation part, break the loop when the input is valid.

Something like this will do.

def reset_password():

    done = False
    while not done:
        sid = input('Student ID: ')
        try:
            sid = int(sid)
            import math
            digits = int(math.log10(sid)) + 1
            if digits != 9:
                print('Error: Student ID Not Found')

            else:
                done = True
        except:
            print('Error: Please enter your Student ID Number.')
Sign up to request clarification or add additional context in comments.

1 Comment

Thx bro, now I understand it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.