an error in commented code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    an error in commented code?

    Here's my code, with the error following it:

    props = ['A', 'B', 'C', 'D']
    group1 = ['C', 'A', 'D', 'B', 17]
    group2 = ['A', 'B', 'D', 'C', 32]
    group3 = ['D', 'B', 'C', 'A', 34]
    group4 = ['B', 'A', 'C', 'D', 17]

    # Submitter: Michael Davies
    def all_perms(str):
    if len(str) <=1:
    yield str
    else:
    for perm in all_perms(str[1:]):
    for i in range(len(perm) +1):
    #nb str[0:1] works in both string and list contexts
    yield perm[:i] + str[0:1] + perm[i:]

    def checkOrder(x, y):
    x_votes = 0
    y_votes = 0

    if group1.index(x) < group1.index(y) :
    x_votes += group1[4]
    else:
    y_votes += group1[4]

    if group2.index(x) < group2.index(y) :
    x_votes += group2[4]
    else:
    y_votes += group2[4]

    if group3.index(x) < group3.index(y) :
    x_votes += group3[4]
    else:
    y_votes += group3[4]

    if group4.index(x) < group4.index(y) :
    x_votes += group4[4]
    else:
    y_votes += group4[4]

    if x_votes > y_votes:
    return x
    else:
    return y

    for order in all_perms(props ):
    # if reduce(checkOrd er, order) == 'A':
    # print 'A wins:'
    # print order
    # if reduce(checkOrd er, order) == 'B':
    # print 'B wins:'
    # print order
    if reduce(checkOrd er, order) == 'C':
    print 'C wins:'
    print order
    # if reduce(checkOrd er, order) == 'D':
    # print 'D wins:'
    # print order

    [color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    *** Error in script or command!

    Traceback (most recent call last):
    File "C:\Python24\my scripts\ecco\1-1-1.py", line 60
    # print order
    ^
    SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Line 60 also happens to be the very last line, which is commented. I've
    tried a couple of different things to see if it's a whitespace problem,
    but it doesn't seem to be. I'm just confused why it detects an error in
    the commented code anyway.

    Thanks.
  • André

    #2
    Re: an error in commented code?

    Tried adding a "\n" (i.e. press the enter key) after the last line
    (i.e. right after the word "order") so that you don't end with a
    comment.

    André

    Comment

    • John Salerno

      #3
      Re: an error in commented code?

      André wrote:[color=blue]
      > Tried adding a "\n" (i.e. press the enter key) after the last line
      > (i.e. right after the word "order") so that you don't end with a
      > comment.
      >
      > André
      >[/color]

      Weird, but it worked! :)

      Comment

      • Gerard Flanagan

        #4
        Re: an error in commented code?

        John Salerno wrote:
        [color=blue]
        > Here's my code, with the error following it:
        >
        > props = ['A', 'B', 'C', 'D']
        > group1 = ['C', 'A', 'D', 'B', 17]
        > group2 = ['A', 'B', 'D', 'C', 32]
        > group3 = ['D', 'B', 'C', 'A', 34]
        > group4 = ['B', 'A', 'C', 'D', 17]
        >
        > # Submitter: Michael Davies
        > def all_perms(str):
        > if len(str) <=1:
        > yield str
        > else:
        > for perm in all_perms(str[1:]):
        > for i in range(len(perm) +1):
        > #nb str[0:1] works in both string and list contexts
        > yield perm[:i] + str[0:1] + perm[i:]
        >
        > def checkOrder(x, y):
        > x_votes = 0
        > y_votes = 0
        >
        > if group1.index(x) < group1.index(y) :
        > x_votes += group1[4]
        > else:
        > y_votes += group1[4]
        >
        > if group2.index(x) < group2.index(y) :
        > x_votes += group2[4]
        > else:
        > y_votes += group2[4]
        >
        > if group3.index(x) < group3.index(y) :
        > x_votes += group3[4]
        > else:
        > y_votes += group3[4]
        >
        > if group4.index(x) < group4.index(y) :
        > x_votes += group4[4]
        > else:
        > y_votes += group4[4]
        >
        > if x_votes > y_votes:
        > return x
        > else:
        > return y
        >
        > for order in all_perms(props ):
        > # if reduce(checkOrd er, order) == 'A':
        > # print 'A wins:'
        > # print order
        > # if reduce(checkOrd er, order) == 'B':
        > # print 'B wins:'
        > # print order
        > if reduce(checkOrd er, order) == 'C':
        > print 'C wins:'
        > print order
        > # if reduce(checkOrd er, order) == 'D':
        > # print 'D wins:'
        > # print order
        >
        >[/color]

        I don't think you need all those 'ifs'.

        groups = [group1, group2, group3, group4]

        def checkOrder(x, y):
        x_votes = 0
        y_votes = 0
        for group in groups:
        if group.index(x) < group.index(y):
        x_votes += group[4]
        else:
        y_votes += group[4]

        for order in all_perms(props ):
        winner = reduce(checkOrd er, order)
        print '%s wins!' % winner
        print order

        Gerard

        Comment

        • John Salerno

          #5
          Re: an error in commented code?

          Gerard Flanagan wrote:
          [color=blue]
          > I don't think you need all those 'ifs'.
          >
          > groups = [group1, group2, group3, group4]
          >
          > def checkOrder(x, y):
          > x_votes = 0
          > y_votes = 0
          > for group in groups:
          > if group.index(x) < group.index(y):
          > x_votes += group[4]
          > else:
          > y_votes += group[4]
          >
          > for order in all_perms(props ):
          > winner = reduce(checkOrd er, order)
          > print '%s wins!' % winner
          > print order
          >[/color]

          Nice. I was shuddering as I copy and pasted each if block. :)

          Comment

          Working...