Question

I cant get my code to return back to asking the user for their email.

I have tried several things, but cant figure it out. Right now it continues on to the next function.

how can i get my code to return to asking for user email until its set to true?

def name_email(name):

correct_email = False
email = str(input('what is your email: '))
print()

try:
    while correct_email == False:
        if '@' in email:
            print('name and email accepted \n')
            print('name: ',name)
            print('email: ',email)
            print()
            correct_email = True 
        else:
            print('email invalid, please try again \n')
            return 
except:
    raise ValueError
Was it helpful?

Solution 3

You can try this code :

def name_email(name):
   correct_email = False
   while correct_email == False:
      email = input('what is your email: ')
      if '@' in email:
          print('name and email accepted \n')
          print('name: ',name)
          print('email: ',email)
          correct_email = True 
      else:
          print('email invalid, please try again \n')
   return email

OTHER TIPS

If you're using Python 2.x use raw_input() instead of input() because input() tries to evaluate whatever you enter.

I think you're using Python 3.x though because of the prints with parentheses. So keep using input()

You don't even need to do the if else condition, you can just put it in the while condition.

def name_email(name):

    correct_email = False
    email = str(input('what is your email: '))
    print()

    try:
        while '@' not in email:
            print('email invalid, please try again \n')
            email = str(input('what is your email: '))
            #why return if you want it to ask them again?
            #return
    except:
        raise ValueError    

    print('name and email accepted \n')
    print('name: ',name)
    print('email: ',email)
    print()

That stray return inside your else statement is what's causing your problem. Try removing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top