Tuple Addition with '+' operand failed without notification

When I was doing this Lab: Build a Player Interface, I wrote this:

    def make_move(self):
        qq = random.choice(self.moves)
        self.position +=qq  # later I saw this is a bad code
        .....

There was NO ERROR but turned out (reading this forums topics) the remedy is:

  self.position = (qq[0]+self.position[0], qq[1]+self.position[1])

Why? if ‘+’ is not supported, then why there is no error? If supported, it should do it or tell me that it can not do it. It just ignored that line.

Please share your complete code, otherwise it might be hard or impossible to determine what’s happening.

when the tests were developed, there was no way to accept every possible valid solution, as in no one is able to think of all of them. If you think your solution is correct and should be accepted please create an issue on github

Thank you for your response. I will do that.

I tested my code again. This time comparing the outputs of the two ways.
Turn out the “+” operator works but not in a way I thought. I expected to add x to x, and y to y. However, the “+” operator just appends the numbers to the existing tuple and makes it longer.
At the time of solving the Lab, I did not know that this was not what expected. It shows a path and collection of movements. After solving the lab, NOW i know it was not a valid answer.

The source code is here( @sanity ):

from abc import ABC, abstractmethod
import random

class Player(ABC):
    def __init__(self):
        self.moves= []
        self.position=(0,0)
        self.path =[self.position]

    def make_move(self):
        print("\n_______________________\n Moves:",self.moves)
        qq = random.choice(self.moves)
        #self.position +=qq  # Line 13
        self.position = (qq[0]+self.position[0], qq[1]+self.position[1]) #Line 14
        #print('self.position=',self.position)
        self.path.append(self.position) 
        return self.position
    
    @abstractmethod
    def level_up(self):
        pass

class Pawn(Player):
    def __init__(self):
        super().__init__()
        self.moves += [(0,1), (1,0),(-1,0),(0,-1)]
    
    def level_up(self):
        self.moves +=[(1,1), (1,-1),(-1,1),(-1,-1)] 


#if __name__ == '__main()__':
pawn = Pawn()
print(type(Player))
print("\n\n", type(pawn),'\n\n')
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print(" pawn.make_move() => ",pawn.make_move())
print('=====finished=====')

If I run it with “Line 14” and comment out “Line 13” the output is:

= RESTART: C:/Users/A/Forum, Build a Player Interface.py
<class 'abc.ABCMeta'>


 <class '__main__.Pawn'> 



_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (-1, 0)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (-1, 1)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (0, 1)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (-1, 1)
=====finished=====

Which is acceptable.

If I run it with “Line 13” and comment out “Line 14” the output is:

= RESTART: C:/Users/A/Forum, Build a Player Interface.py
<class 'abc.ABCMeta'>


 <class '__main__.Pawn'> 



_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (0, 0, 1, 0)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (0, 0, 1, 0, 0, 1)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (0, 0, 1, 0, 0, 1, 0, -1)

_______________________
 Moves: [(0, 1), (1, 0), (-1, 0), (0, -1)]
 pawn.make_move() =>  (0, 0, 1, 0, 0, 1, 0, -1, 0, 1)
=====finished=====

Which was not desired.

I consider this topic is to be closed and I am thankful for the help.

@ILM I expected the test system to check my code as a whole in Lab. In Workshop lessons I would accept your idea. But in a Lab I can write anything and the overall output of a function is examined. If I am wrong, would you please explain more?

it depends on the lab, some check you use something specific, others check your output only