我是新手。我想将用户名和设备 ID 作为存储在变量中的 2 个单独的用户输入。
我尝试对两个访问列表建立索引。我会根据访问列表检查用户输入和设备 ID 输入。我认为列表的索引是必要的,因为如果我输入“Chad”作为用户,输入“127”(都在索引 0 处)作为设备 ID,那就没问题了。如果我输入“Chad”作为用户,输入“416”(索引 0 和索引 1)作为设备 ID,也将允许这样做。
只要输入在列表中的索引完全相同,我就需要根据访问列表检查输入。
问题:如果输入正确的用户名,则授予访问权限,如果输入任何设备 ID,它也会授予访问权限,这是我不想要的。
代码片段如下:
approved_users = ['Chad', 'Tina', 'Jared', 'Lars', 'Henry', 'Jasmine']
device_id = [127, 416, 709, 358, 672, 939]
user = input('Enter your username: ')
device = input('Enter your device id: ')
我尝试对访问列表建立索引,然后将用户输入与它们进行比较,看看每个列表中的索引是否相同,但不太正确。
不要使用并行列表和索引,而是尝试使用字典来查找每个用户的值。
示例:
# Create mapping of users to device id
users_ids = {
'Chad': 127,
'Tina': 416,
'Jared': 709,
'Lars': 358,
'Henry': 672,
'Jasmine': 939
}
# Get input
user = input('Enter your username: ')
device = input('Enter your device id: ')
# Make sure user ID and device match
if user_ids[user] == device:
print("You are good")
else:
print("No match")