Python Forum
Class matrix novice question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class matrix novice question
#1
Hello,


Part of my homework:

The attribute self.ground is the matrix who represents the ground of the game, it is a list of lists of integers, of dimensions self.height lines and self.width columns. The initial values are -2 in the first four lines and -1 on the rest of the ground. A negative value show that the case is empty

I don't see at all what i must write.
Reply
#2
It doesn't look like you've provided enough information here.
Reply
#3
width=10
height=8

# You can create the lines like this
# lineOfMinusTwo=[-2]*width
# lineOfMinusOne=[-1]*width

# So:

ground=[] # Start with empty
# lines of -2 for the first 4
for _ in range(4):
    ground.append([-2]*width)
# lines of -1 for the rest
for _ in range(4,height):
    ground.append([-1]*width)


When you are a tattooed python programmer who wants to show off your one-liner mad skillz, the same thing can be done with:
ground=[[[-1,-2][i<4]]*width for i in range(height)]
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
Ok thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,328 May-15-2019, 01:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020