Home > front end >  Is there anyway to modify a variable inside a python try
Is there anyway to modify a variable inside a python try

Time:07-14

I tried to add global after the try and it didn't work I want to fix the code to work like this : if Kouki variable contains "webmail", the webmailpage variable = kouki and after that I can use the new value of the webmailpage variable outside the try and except thanks !

webmailpage=""
for cookie in e:
  try:

      Kouki=cookie["domain"]
      webmail="webmail"
      if Kouki.find(webmail)!= -1 :
          webmailpage=Kouki

  except:
     continue
print(webmailpage)

CodePudding user response:

According to your description no try except is needed at all.

Just use a conditional statement.

webmailpage = ""
kouki = cookie.get("domain","")
if "webmail" in kouki:
    webmailpage = kouki
print(webmailpage)

Unless you expect cookie to not contain the "domain" key in which case you could do this:

webmailpage = ""
try:
    kouki = cookie.get("domain","")
    if "webmail" in kouki:
        webmailpage = kouki
except KeyError:
    pass
print(webmailpage)
  • Related