-3

Write a program, that allows a user to input integer values and query a 2-dimensional array of size 9x9. Your program should then ask the user for a pair of coordinates,(x,y), separated by a space and return the value at the position specified by the given coordinates. For instance, 0 3 should return the value 7 (the value at row one, column four – bearing in mind that array indices start at zero). Assume that each integer is a single digit from 1 - 9. Enter -1 for either coordinate to end the program.

Sample I/O:
Enter an array:
359716482
867345912
413928675
398574126
546281739
172639548
984163257
621857394
735492861

Enter coordinates: 0 3
Value = 7
Enter coordinates: 5 5
Value = 9
Enter coordinates: 8 8
Value = 1
Enter coordinates: -1 -1
DONE

This is my code so far:

arr = input("Enter an array:\n")

coordinates = input("Enter coordinates:\n")
x = coordinates[:1]
y = coordinates[2:3]

while x!= -1 or x!= -1 :

x = coordinates[:1]
y = coordinates[2:3]
x = int(x),
y = int(y)
value = arr[x][y] 
print("Value = "+ arr)

It is very rough. I understand what I need to do and I have an idea of how to do it but the actual programming I struggle with. my idea is to create a sentinal loop that exits once the user enters -1 for a coordinate. using a while loop we ask for coordinates and then cut the string to get the individual x and y coordinates then use it to find the number in the specified index.

  • You might want to start out by checking out what is actually in `arr`. – Scott Hunter May 24 '21 at 19:56
  • Are you sure you copied the sample correctly? Shouldn't it be `Enter coordinates: 0 3` with a space between them? – Barmar May 24 '21 at 19:57
  • Why are you checking for `DONE`? The instructions clearly say that the sentinel for the end is `-1` as one of the coordinates. – Barmar May 24 '21 at 19:58
  • 2
    Hello, Isabella, welcome to Stack Overflow. Just some advice, when asking for debugging help, you should provide 1) the problem specification and desired behavior (good job providing those!) 2) The behavior you are *actually* getting. Also, check out [ask] and the [help] for advice on things like question titles... it is important to understand this isn't a forum like reddit, there are guidelines to how questions should be structured – juanpa.arrivillaga May 24 '21 at 19:59
  • 1
    Start by reading the task description clearly. You're not reading 9 lines of input for the array. You're not splitting the coordinates with a space between each. – Barmar May 24 '21 at 20:01

1 Answers1

1

I've annotated the code below with the steps of the instructions.

print("Enter an array")
# read 9 lines into the list
arr = [input() for _ in range(9)] 

while True:
    # Read a line of coordinates, split into two elements, convert to integers
    x, y = map(int, input("Enter coordinates: ").split(' ', 2))
    # Stop if sentinel in either coordinate
    if -1 in (x, y):
        print("DONE")
        break
    # print the element at the specified coordinates
    print(f"Value = {arr[x][y]}")
Barmar
  • 741,623
  • 53
  • 500
  • 612