Reputation: 493
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()
Upvotes: 0
Views: 207
Reputation: 14229
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()
Upvotes: 1