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?

2

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.