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.
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()
CodePudding user response:
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"
...
CodePudding user response:
If number < 1
, then number <= 7
, so the first condition in your if
statements will apply before it reaches the number < 1
check. The remainder in that case is 0
, so it will index matrix1[0][-1]
, which is the last item in that row. You should move the number < 1
check to the beginning of your if
statements to fix this. Also, I think you have a typo in that code since you're accessing matrix
instead of matrix1
, and you're using the ==
operator instead of an assignment.
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"
CodePudding user response:
You can do this more simply by just using int division for the first index rather than having a bunch of if
statements that individually map each range to the same result:
def matrix():
print("---The Matrix---")
# create a 2d array of 7x7 stars
matrix1 = [["*" for _ in range(7)] for _ in range(7)]
number = int(input("Enter number: "))
matrix1[number // 7][number % 7] = "X"
# print the matrix
print('\n'.join(''.join(row) for row in matrix1))
matrix()
---The Matrix---
Enter number: 0
X******
*******
*******
*******
*******
*******
*******
---The Matrix---
Enter number: 48
*******
*******
*******
*******
*******
*******
******X
If you want the user-inputted numbers to go from 1-49 instead of 0-48, just subtract 1 from number
after the user inputs it. You can "clip" inputs below 1 and above 49 with min
and max
:
number = int(input("Enter number: ")) - 1
number = max(number, 0)
number = min(number, 48)