Py - 使用输入打印数组的数据

问题描述 投票:1回答:1

我正在做一个计算机科学过去的论文(NEA),我有一个问题,我将数据存储在一个多维数组中,我问用户的输入,其中预期的输入已经在数组中,我想要程序打印出存储输入的数组。

# 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: ")

这是我需要帮助的地方,如果输入的ID是“San12318”,我怎样才能让程序打印出存储它的数组?

python multidimensional-array
1个回答
1
投票

如何在for循环中检查列表中每个数据记录的第0个索引处的值,即ID值:

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()

示例用法1:

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

用法示例2:

Please enter a user ID: Gat42119
Error no record was found for the input user ID: Gat42119
© www.soinside.com 2019 - 2024. All rights reserved.