0

I was trying to create a signup function which will be prompting user fews information. The code below is partial part of the function. I'm using while loop to prevent any invalid input or specific input to perform other task. Once the input validation is done, I used for loop to check for registered email. However, I'm using recursive function when the registering email has been registered, is there a way that I can avoid the resursive function and put the for loop inside the while loop instead ?

user = [['a','a.gmail.com'],['b','b.gmail.com'],['c','c.gmail.com']]

def signup():
     print("\ Enter '0' to return mainpage")
     emailAddress = input("Please enter your email address   : ")
     while True:
          if emailAddress == '0':
               print('mainpage returned')
               signup()
          elif emailAddress == '':
               emailAddress = input("Please enter your email address   : ")
          else:
               break
     # check if email address is registered or not
     for item in user:
          if emailAddress == item[1]:
               print("This Email address has been registered")
               signup()
     else:
          print("email registering:",emailAddress)
signup()               
               
3
  • 1
    You should not recurse at all. Always break out to the lowest block in the signup(). for item in user should be inside the while loop, where the current break statement is. Commented May 27, 2021 at 9:05
  • May I know when to recurse the function and when to keep using the iteration ? Commented May 27, 2021 at 9:21
  • 1
    Do not recurse. Period. Break out of the while loop when your conditions are satisfied. Commented May 27, 2021 at 10:14

1 Answer 1

1

Do not use recursion. Something like this will do. Your main issue is the for-loop.

user = [['a','a.gmail.com'],['b','b.gmail.com'],['c','c.gmail.com']]

def signup():
    
    emailAddress = None
    
    #---
    
    while True:
        print("\ Enter '0' to return mainpage")
        
        desiredEmailAddress = input("Please enter your email address   : ")
        
        if desiredEmailAddress == '0':
            print('Ok, will return to mainpage')
            break # <---- BREAK OUT OF THE WHILE LOOP
        
        elif desiredEmailAddress == '':
            pass # let the while-loop iterate again
        
        else:
            # check if email address is registered or not
            emailIsAlreadyRegistered = False
            
            for item in user:
                if desiredEmailAddress == item[1]:
                    emailIsAlreadyRegistered = True
                    break # we can break out of the for loop
            
            if emailIsAlreadyRegistered:
                print("This Email address has been registered")
                pass # let the while-loop iterate again
            else:
                emailAddress = desiredEmailAddress
                break # <---- BREAK OUT OF THE WHILE LOOP
    
    #---
    
    if emailAddress is None:
        # User wanted to return to mainpage
        return None
    else:
        print("email registering:", emailAddress)
        return emailAddress
    
signup()

Sign up to request clarification or add additional context in comments.

Comments

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.