-2

Consider the following xml file (lieferungen2.xml):

<?xml version="1.0"?>
<lieferungen>
    <artikel id="1">
       <name>apple</name>
       <preis >2</preis>
       <lieferant>Friedman</lieferant>
    </artikel>
</lieferungen>

With the following code, I wanted to print "apple" to the command line:

import xml.dom.minidom 

dom = xml.dom.minidom.parse("lieferungen2.xml")

a = dom.getElementsByTagName("artikel")

num=0


while(True):

    if a[0].childNodes[num].nodeName != "name":
        num++
    else:
        break

print(a[0].childNodes[num].childNodes[0].nodeValue)

However, I get the following error message:

    num++
        ^
SyntaxError: invalid syntax

To me this syntax looks perfectly fine? What's wrong here?

3
  • 1
    You should use num += 1 instead Commented Dec 25, 2016 at 0:11
  • 1
    It's marked as a syntax error because it's not Python syntax. Commented Dec 25, 2016 at 0:19
  • You can also refer to stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python to have an idea why increment operators are not a part of Python. Commented Dec 25, 2016 at 0:27

2 Answers 2

0

num++ is not valid Python code, it would be like this

num += 1
Sign up to request clarification or add additional context in comments.

Comments

0

Python does not support x ++ to increase a variable by one.

Instead, you have to do x += 1.

So your code would be:

if a[0].childNodes[num].nodeName != "name":
    num += 1
else:
    break

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.