postfix to binaryExpression list












0















I am new to python.
Error appears when I run this code:



list=
list2=
list.append("p")
list.append("&")
list.append("k")
print(list)
operator="&" or "|" or">" or "=" or "~"
prop="p" or "q " or "r"
#a=0
for i in list:
if i == operator:
# a = list.index(i)
# print(a - 1)
nextelem = list[list.index(i) + 1]
prevelem = list[list.index(i) - 1]
print(nextelem)
print(prevelem)
list.remove(i)
list2.append(i)
if nextelem==prop:
print("voici",nextelem)
list2.append(nextelem)
list2.append(prevelem)
print(list2)


The error message specified:




Traceback (most recent call last):



File "C:/Users/PC/PycharmProjects/LIAATP1/TP01.py", line 114, in
if nextelem==prop:
NameError: name 'nextelem' is not defined




for example, with a formula: p & q | r



Expected return:



[|,&,r,p,q]



[root,leftchild,rightchild,leftchildof'&',rightchildof'&']










share|improve this question





























    0















    I am new to python.
    Error appears when I run this code:



    list=
    list2=
    list.append("p")
    list.append("&")
    list.append("k")
    print(list)
    operator="&" or "|" or">" or "=" or "~"
    prop="p" or "q " or "r"
    #a=0
    for i in list:
    if i == operator:
    # a = list.index(i)
    # print(a - 1)
    nextelem = list[list.index(i) + 1]
    prevelem = list[list.index(i) - 1]
    print(nextelem)
    print(prevelem)
    list.remove(i)
    list2.append(i)
    if nextelem==prop:
    print("voici",nextelem)
    list2.append(nextelem)
    list2.append(prevelem)
    print(list2)


    The error message specified:




    Traceback (most recent call last):



    File "C:/Users/PC/PycharmProjects/LIAATP1/TP01.py", line 114, in
    if nextelem==prop:
    NameError: name 'nextelem' is not defined




    for example, with a formula: p & q | r



    Expected return:



    [|,&,r,p,q]



    [root,leftchild,rightchild,leftchildof'&',rightchildof'&']










    share|improve this question



























      0












      0








      0








      I am new to python.
      Error appears when I run this code:



      list=
      list2=
      list.append("p")
      list.append("&")
      list.append("k")
      print(list)
      operator="&" or "|" or">" or "=" or "~"
      prop="p" or "q " or "r"
      #a=0
      for i in list:
      if i == operator:
      # a = list.index(i)
      # print(a - 1)
      nextelem = list[list.index(i) + 1]
      prevelem = list[list.index(i) - 1]
      print(nextelem)
      print(prevelem)
      list.remove(i)
      list2.append(i)
      if nextelem==prop:
      print("voici",nextelem)
      list2.append(nextelem)
      list2.append(prevelem)
      print(list2)


      The error message specified:




      Traceback (most recent call last):



      File "C:/Users/PC/PycharmProjects/LIAATP1/TP01.py", line 114, in
      if nextelem==prop:
      NameError: name 'nextelem' is not defined




      for example, with a formula: p & q | r



      Expected return:



      [|,&,r,p,q]



      [root,leftchild,rightchild,leftchildof'&',rightchildof'&']










      share|improve this question
















      I am new to python.
      Error appears when I run this code:



      list=
      list2=
      list.append("p")
      list.append("&")
      list.append("k")
      print(list)
      operator="&" or "|" or">" or "=" or "~"
      prop="p" or "q " or "r"
      #a=0
      for i in list:
      if i == operator:
      # a = list.index(i)
      # print(a - 1)
      nextelem = list[list.index(i) + 1]
      prevelem = list[list.index(i) - 1]
      print(nextelem)
      print(prevelem)
      list.remove(i)
      list2.append(i)
      if nextelem==prop:
      print("voici",nextelem)
      list2.append(nextelem)
      list2.append(prevelem)
      print(list2)


      The error message specified:




      Traceback (most recent call last):



      File "C:/Users/PC/PycharmProjects/LIAATP1/TP01.py", line 114, in
      if nextelem==prop:
      NameError: name 'nextelem' is not defined




      for example, with a formula: p & q | r



      Expected return:



      [|,&,r,p,q]



      [root,leftchild,rightchild,leftchildof'&',rightchildof'&']







      python list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 6:55









      Aqueous Carlos

      293213




      293213










      asked Nov 12 '18 at 23:01









      Yacine BenatiaYacine Benatia

      52




      52
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Strings evaluate to True in Python unless they're empty, so operator here has the value '&' (since or will return the first argument to it that evaluates to True). Testing it on the command line:



          operator = "&" or "|" or ">" or "=" or "~"
          print(operator)
          # '&'


          So every time you loop over list (which you should rename, since list is a reserved word), you check whether i == '&'. Since it never does, the code in your first if block is never executed; since that code is never executed, nextelem is never set.



          Which is why you end up with this:




          NameError: name 'nextelem' is not defined






          Regarding the rest of your code, the logic has fundamental issues that will prevent it from doing what you expect, even if you do get it to run without throwing an exception.



          Before you attempt to rewrite it, I would strongly suggest going through the official Python tutorial to gain a firm understanding of the data and control structures you'll need to implement that algorithm.





          Solely for the sake of providing an example of valid list handling, and ignoring the '~' operator and operator precedence...



          input_string = "p & q | r"

          values = input_string.split(" ")
          print(values)
          # ['p', '&', 'q', '|', 'r']

          print(values[-2::-2] + values[-1::-2])
          # ['|', '&', 'r', 'q', 'p']


          values[-1] gives the last element in a list. values[-1::-2] returns an array slice starting at the end and stepping backwards by 2 elements at a time until all elements have been processed. So, in this case, that will result in ['r', 'q', 'p'].



          values[-2], then, will start at the second to last element in the list. Stepping back by two again, values[-2::-2] returns ['|', '&']. Concatenating these lists gives you the result you're looking for, though only in very specific circumstances; a general-case solution is something you'll need to work on once you're comfortable with the basics of Python (and come back to SO when you have specific questions/issues).






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271345%2fpostfix-to-binaryexpression-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Strings evaluate to True in Python unless they're empty, so operator here has the value '&' (since or will return the first argument to it that evaluates to True). Testing it on the command line:



            operator = "&" or "|" or ">" or "=" or "~"
            print(operator)
            # '&'


            So every time you loop over list (which you should rename, since list is a reserved word), you check whether i == '&'. Since it never does, the code in your first if block is never executed; since that code is never executed, nextelem is never set.



            Which is why you end up with this:




            NameError: name 'nextelem' is not defined






            Regarding the rest of your code, the logic has fundamental issues that will prevent it from doing what you expect, even if you do get it to run without throwing an exception.



            Before you attempt to rewrite it, I would strongly suggest going through the official Python tutorial to gain a firm understanding of the data and control structures you'll need to implement that algorithm.





            Solely for the sake of providing an example of valid list handling, and ignoring the '~' operator and operator precedence...



            input_string = "p & q | r"

            values = input_string.split(" ")
            print(values)
            # ['p', '&', 'q', '|', 'r']

            print(values[-2::-2] + values[-1::-2])
            # ['|', '&', 'r', 'q', 'p']


            values[-1] gives the last element in a list. values[-1::-2] returns an array slice starting at the end and stepping backwards by 2 elements at a time until all elements have been processed. So, in this case, that will result in ['r', 'q', 'p'].



            values[-2], then, will start at the second to last element in the list. Stepping back by two again, values[-2::-2] returns ['|', '&']. Concatenating these lists gives you the result you're looking for, though only in very specific circumstances; a general-case solution is something you'll need to work on once you're comfortable with the basics of Python (and come back to SO when you have specific questions/issues).






            share|improve this answer




























              0














              Strings evaluate to True in Python unless they're empty, so operator here has the value '&' (since or will return the first argument to it that evaluates to True). Testing it on the command line:



              operator = "&" or "|" or ">" or "=" or "~"
              print(operator)
              # '&'


              So every time you loop over list (which you should rename, since list is a reserved word), you check whether i == '&'. Since it never does, the code in your first if block is never executed; since that code is never executed, nextelem is never set.



              Which is why you end up with this:




              NameError: name 'nextelem' is not defined






              Regarding the rest of your code, the logic has fundamental issues that will prevent it from doing what you expect, even if you do get it to run without throwing an exception.



              Before you attempt to rewrite it, I would strongly suggest going through the official Python tutorial to gain a firm understanding of the data and control structures you'll need to implement that algorithm.





              Solely for the sake of providing an example of valid list handling, and ignoring the '~' operator and operator precedence...



              input_string = "p & q | r"

              values = input_string.split(" ")
              print(values)
              # ['p', '&', 'q', '|', 'r']

              print(values[-2::-2] + values[-1::-2])
              # ['|', '&', 'r', 'q', 'p']


              values[-1] gives the last element in a list. values[-1::-2] returns an array slice starting at the end and stepping backwards by 2 elements at a time until all elements have been processed. So, in this case, that will result in ['r', 'q', 'p'].



              values[-2], then, will start at the second to last element in the list. Stepping back by two again, values[-2::-2] returns ['|', '&']. Concatenating these lists gives you the result you're looking for, though only in very specific circumstances; a general-case solution is something you'll need to work on once you're comfortable with the basics of Python (and come back to SO when you have specific questions/issues).






              share|improve this answer


























                0












                0








                0







                Strings evaluate to True in Python unless they're empty, so operator here has the value '&' (since or will return the first argument to it that evaluates to True). Testing it on the command line:



                operator = "&" or "|" or ">" or "=" or "~"
                print(operator)
                # '&'


                So every time you loop over list (which you should rename, since list is a reserved word), you check whether i == '&'. Since it never does, the code in your first if block is never executed; since that code is never executed, nextelem is never set.



                Which is why you end up with this:




                NameError: name 'nextelem' is not defined






                Regarding the rest of your code, the logic has fundamental issues that will prevent it from doing what you expect, even if you do get it to run without throwing an exception.



                Before you attempt to rewrite it, I would strongly suggest going through the official Python tutorial to gain a firm understanding of the data and control structures you'll need to implement that algorithm.





                Solely for the sake of providing an example of valid list handling, and ignoring the '~' operator and operator precedence...



                input_string = "p & q | r"

                values = input_string.split(" ")
                print(values)
                # ['p', '&', 'q', '|', 'r']

                print(values[-2::-2] + values[-1::-2])
                # ['|', '&', 'r', 'q', 'p']


                values[-1] gives the last element in a list. values[-1::-2] returns an array slice starting at the end and stepping backwards by 2 elements at a time until all elements have been processed. So, in this case, that will result in ['r', 'q', 'p'].



                values[-2], then, will start at the second to last element in the list. Stepping back by two again, values[-2::-2] returns ['|', '&']. Concatenating these lists gives you the result you're looking for, though only in very specific circumstances; a general-case solution is something you'll need to work on once you're comfortable with the basics of Python (and come back to SO when you have specific questions/issues).






                share|improve this answer













                Strings evaluate to True in Python unless they're empty, so operator here has the value '&' (since or will return the first argument to it that evaluates to True). Testing it on the command line:



                operator = "&" or "|" or ">" or "=" or "~"
                print(operator)
                # '&'


                So every time you loop over list (which you should rename, since list is a reserved word), you check whether i == '&'. Since it never does, the code in your first if block is never executed; since that code is never executed, nextelem is never set.



                Which is why you end up with this:




                NameError: name 'nextelem' is not defined






                Regarding the rest of your code, the logic has fundamental issues that will prevent it from doing what you expect, even if you do get it to run without throwing an exception.



                Before you attempt to rewrite it, I would strongly suggest going through the official Python tutorial to gain a firm understanding of the data and control structures you'll need to implement that algorithm.





                Solely for the sake of providing an example of valid list handling, and ignoring the '~' operator and operator precedence...



                input_string = "p & q | r"

                values = input_string.split(" ")
                print(values)
                # ['p', '&', 'q', '|', 'r']

                print(values[-2::-2] + values[-1::-2])
                # ['|', '&', 'r', 'q', 'p']


                values[-1] gives the last element in a list. values[-1::-2] returns an array slice starting at the end and stepping backwards by 2 elements at a time until all elements have been processed. So, in this case, that will result in ['r', 'q', 'p'].



                values[-2], then, will start at the second to last element in the list. Stepping back by two again, values[-2::-2] returns ['|', '&']. Concatenating these lists gives you the result you're looking for, though only in very specific circumstances; a general-case solution is something you'll need to work on once you're comfortable with the basics of Python (and come back to SO when you have specific questions/issues).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 7:44









                kungphukungphu

                2,75111324




                2,75111324






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271345%2fpostfix-to-binaryexpression-list%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Coverage of Google Street View

                    Full-time equivalent

                    Surfing