Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

2d array Python question, range of if, elifs, one not working as expected

I have the following program in which the user enters a number an an X is placed in the position on the matrix.

I am attempting, for teaching purposes, to solve it using selection only (at the moment).

You’ll note that it works for all numbers, except 0. The last elif is for numbers less than 1 -e.g. 0. The code tells it to place the X in position 0,0, but it places it in 7 instead.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What’s going on?
https://trinket.io/python/987e8c46d7

Note, I only added the last elif, because the first elif which should also deal with numbers less than 7 doesn’t work. How can I deal with this using the same structure.

I want to then build on the teaching looking at the if statements and look at areas of repetition or unrequired complexity, in order to show it can be further simplified for more elegant code.

def matrix():
  print("---The Matrix---")
  #create a 1d array of 7 stars
  matrix1=[
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"],
  ["*","*","*","*","*","*","*"]
  ]
  #user enters a number
  number=int(input("Enter number:"))
  #you are always finding the remainder on each row to place the X
  remainder=number%7
  
  #an 'X' is placed in the position of the number
  #remainder-1 because we start at index 0
  
  if number<=7:
    matrix1[0][remainder-1]="X"
  elif number>7 and number<15:
    matrix1[1][remainder-1]="X"
  elif number>14 and number<22:
    matrix1[2][remainder-1]="X"
  elif number>21 and number<29:
    matrix1[3][remainder-1]="X"
  elif number>28 and number<36:
    matrix1[4][remainder-1]="X"
  elif number>35 and number<43:
    matrix1[5][remainder-1]="X"
  elif number>42 and number<50:
    matrix1[6][remainder-1]="X"
  elif number<1:
    matrix[0][0]=="X"


  #the updated matrix is printed.
  print(matrix1)
matrix()

>Solution :

Your el-if statements should be like below. In your implementation, the code goes if number <=7 statement when user enters 0 as input number. So, if number<1: statement should come first.

...

  if number<1:
    matrix1[0][0]="X"
  elif number<=7:
    matrix1[0][remainder-1]="X"
  elif number>7 and number<15:
    matrix1[1][remainder-1]="X"
  elif number>14 and number<22:
    matrix1[2][remainder-1]="X"
  elif number>21 and number<29:
    matrix1[3][remainder-1]="X"
  elif number>28 and number<36:
    matrix1[4][remainder-1]="X"
  elif number>35 and number<43:
    matrix1[5][remainder-1]="X"
  elif number>42 and number<50:
    matrix1[6][remainder-1]="X"

...

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading