【问题标题】:Python: Return FunctionPython:返回函数
【发布时间】:2021-11-19 15:00:30
【问题描述】:

我想了解在发生错误时如何返回或重复设置的代码。

例如:

import smtpd    

LoginE = input("What is the email you will be using? \n")
LoginP = input("what is the password of the email you will using? \n")
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login( LoginE , LoginP)

我尝试使用 try 函数,但它仍然会崩溃并告诉我有错误。

【问题讨论】:

标签: python


【解决方案1】:

你可以用一个while块来包装代码:

import smtpd  
while True:
   LoginE = input("What is the email you will be using? \n")
   LoginP = input("what is the password of the email you will using? \n")
   try: 
      server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
      server.login( LoginE , LoginP)
      break
   except: # you can put any error here
      pass

【讨论】:

  • true 会抛出异常,我想你的意思是True
  • 是的,我是这个意思。我直接把它写到答案上,所以很容易。
猜你喜欢
  • 2013-09-27
  • 2021-05-10
  • 2019-02-12
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多