簡體   English   中英

返回到python 3中的選擇點

[英]returning to a select point in python 3

我無法獲取代碼以返回要求用戶提供電子郵件的信息。

我已經嘗試了幾件事,但無法弄清楚。 現在,它將繼續執行下一個功能。

我如何獲取我的代碼以返回要求用戶提供的電子郵件,直到將其設置為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

如果您使用的是Python 2.x,請使用raw_input()而不是input()因為input()嘗試評估您輸入的內容。

我認為您正在使用Python 3.x,因為帶有括號的打印內容。 因此,請繼續使用input()

您甚至不需要執行if else條件,只需將其放在while條件中即可。

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()

else語句中的錯誤return是導致您出現問題的原因。 嘗試將其刪除。

您可以嘗試以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM