简体   繁体   中英

How to make this script loop until there are no errors python?

I'm trying to make this script loop until there are no errors but I'm pretty new to python and just keep encountering errors

I've tried everything I know but this isnt an easy task for me

  def change_screen_name(self):
       print("Attempting change...")
       try:
           status = self.api.update_profile(screen_name="name")
           print("Name updated!")
       except tweepy.TweepError as error:
           resp = error.response.json()["errors"][0]
           print("Name unavailable.")
           print("{} ({})".format(resp["message"], resp["code"]))
       finally:
           return self

Expected result is to continue attempting the same namechange until there are no errors but it currently only tries once then stops

def change_screen_name(self):
    while True:
        print("Attempting change...")
        try:
            status = self.api.update_profile(screen_name="name")
            print("Name updated!")
            return self
        except tweepy.TweepError as error:
            resp = error.response.json()["errors"][0]
            print("Name unavailable.")
            print("{} ({})".format(resp["message"], resp["code"]))

1) Have an infinite loop running

2) Break out the infinite loop once your attempt succeeds.

3) Finally runs after try/except. If you have a finally statement it will always run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM