1

I am doing a computer science past paper (NEA) and I have a problem, where I have data stored in a multidimensional array, and I ask input from the user, where the expected input is already in the array, and I want the program to print out the array in which the input is stored.

# Array containing the ID, last name, year in, membership, nights booked, points.
array = [["San12318", "Sanchez", 2018, "Silver", 1, 2500],
        ["Gat32119", "Gatignol", 2019, "Silver", 3, 7500]]

# Asking to the user to enter the ID
inUser = input("Please enter the user ID: ")

And this is where I need help, if the ID entered is "San12318", how can I get the program to print out the array where it is stored?

  • Possible duplicate of [Lists of Lists: How can I make a list of lists from user input?](https://stackoverflow.com/questions/15380625/lists-of-lists-how-can-i-make-a-list-of-lists-from-user-input) – aedry Mar 12 '19 at 01:46
  • 1
    `np.argwhere(array == inUser)[0]` – Sheldore Mar 12 '19 at 01:49

1 Answers1

1

How about a for loop that checks the value at the 0th index for each data record in the list i.e. the ID value:

def main():
  records = [["San12318", "Sanchez",  2018, "Silver", 1, 2500],
             ["Gat32119", "Gatignol", 2019, "Silver", 3, 7500]]
  input_user_id = input("Please enter a user ID: ")
  print(find_user_id(records, input_user_id.title()))

def find_user_id(records, user_id):
  for record in records:
    if record[0] == user_id:
      return f"Found associated record: {record}"
  return f"Error no record was found for the input user ID: {user_id}"

if __name__ == "__main__":
  main()

Example Usage 1:

Please enter a user ID: san12318
Found associated record: ['San12318', 'Sanchez', 2018, 'Silver', 1, 2500]

Example Usage 2:

Please enter a user ID: Gat42119
Error no record was found for the input user ID: Gat42119
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40