TORONTO KIDS COMPUTER CLUB | Monday 20:00 Python Practice 20.09.21.
18342

# Monday 20:00 Python Practice 20.09.21.

## 24 Sep Monday 20:00 Python Practice 20.09.21.

Question：
Create a Tank class which have the attributes of:

• name
• direction(use ‘up’, ‘down’, ‘left’ or ‘right’)
• x position and
• y position

The class also have the following method:

• move (direction, steps) method can change the x or y position base on the direction and steps. it will also change the direction tank facing. (‘up’ and ‘down’ can change y position while ‘left’ and ‘right’ can change x position)
• Using __init__() to Initial the tank to make sure it start from x position of 0, y position of 0 and facing up
•  __str__() method

You need write main program to create tank object from Tank class, you need to use the method of move and use print(tank object) to print out the tank’s current x, y position and its direction after each move.

• ##### Jesse Shi
Posted at 03:48h, 27 September Reply

class Tank:
def __init__(self, name, postion=’x’, position=’y’, direction=’up’):
self.name = name
self.position = position
self.direction = direction

def move(self):
if position == ‘x’:
self.direction = ‘left’ and ‘right’
if position == ‘y’:
self.direction = ‘up’ and ‘down’

t1 = Tank(‘Panzerkampfwagen VVI Maus’)
print(‘tank1 name: %s, position: %s, direction: %s’
%(t1.name, t1.position, t1.direction))

• ##### Ethan Li
Posted at 23:16h, 28 September Reply

”’
Tank:
attributes:
1.name
2.position
3.direction
methods:
1.move(direction,steps)

”’
class Tank:
def __init__(self, name, position, direction=’up’):
self.name = name
self.position = 0
self.direction = direction

def move(self):
direction=’down’
if position == ‘x’:
self.direction = ‘left’ and ‘right’
if position == ‘y’:
self.direction = ‘up’ and ‘down’

def getDirection(self):
return self.direction

def __str__(self):
msg=’name: %s, position: %s, direction: %s’ \
%(t1.name, t1.position, t1.direction)
return msg