我正在做计算机科学前期论文(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”,如何获取程序以打印出存储它的阵列?
答案 0 :(得分: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