Link to home
Start Free TrialLog in
Avatar of Miguel Hernandez
Miguel Hernandez

asked on

How to add strings with "for loop" in Python if they meet a condition.

#I am trying to add strings if they start with the "<" character. I get "cannot assign to a literal" when I press "enter" after teh "return" statement.

def tag_count(xml_tags):
      xml_tags_sum=0
... for "xml_tag" in xml_tags:
          if xml_tag[0]=="<"
                xml_tags+=xml_tag
      return xml_tags_sum

# I suspect the error is in line 2, but I cannot fix it.
SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Miguel Hernandez
Miguel Hernandez

ASKER

Hi Norie:

After your suggestion the functions runs well. However, when I type the arguments, for example:
x=["<wise","<fire","<light"]
tag_count(x)
returns "0".
I do not know what are you trying to solve. Anyway, if this is not just a project to learn things, the standard Python xml.etree.ElementTree is probably much better way to process a XML content.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just searched on the web and tried! It works with a lot of different arguments.