根据用户输入检查 Python List/Dict 并从另一个列表中的相同索引值显示
Check Python List/Dict based on user input & display from the same index value in another list
因此我们要求用户输入一个数字并在列表中匹配该数字:
numbers = [1, 2, 3]
num_words = ['One', 'Two', 'Three']
print('Type a number from 1 -3: ')
user = int(input())
if user in numbers:
print(f"{user} is the digit you typed " + ???)
一旦完成,我想匹配在数字列表中找到的用户键入的内容,并在下一个列表中打印出相应的索引值。
所以用户输入 1 并检查数字,如果找到它,它会打印:一个
从 num_words 列表到用户。
我也试过字典,但无法弄清楚如何根据用户的输入和与字典键的匹配向用户显示匹配值:
nums = {1: 'One', 2: 'Two', 3: 'Three'}
print ('Type 1, 2, or 3: ')
user = int(input())
if user in nums.keys():
print(num.values) #Need it to print the nums.value based on the users input at match to nums.keys
else:
print('Goodbye!')
但是,我的 Google 搜索没有找到任何与我正在尝试做的类似的东西。我在这里找到了主题(例如:, ,但它没有帮助,因为据我所知(以及目前对 Python 的理解)它并没有 100% 匹配我想要的做。
从字典中获取值的语法是 dictionary_name[key]
。如果找不到密钥,这将引发 KeyError
,因此您可以使用 try/except
而不是提前检查并执行 if/else
:
nums = {1: 'One', 2: 'Two', 3: 'Three'}
print ('Type 1, 2, or 3: ')
user = int(input())
try:
print(f"{user} is the digit you typed {nums[user]}")
except KeyError:
print('Goodbye!')
如果您不希望此代码在无效整数输入上引发 ValueError
,您可以只使用字符串作为字典键:
nums = {'1': 'One', '2': 'Two', '3': 'Three'}
print ('Type 1, 2, or 3: ')
user = input()
对于列表的第一个示例,使用 .index()
查找第一个列表中数字的索引位置,然后使用该值查找第二个列表中的项目:
if user in numbers:
location = numbers.index(user)
print(num_words[location])
对于带有字典的第二个示例,只需使用标准字典查找:
if user in nums:
print(nums[user])
在这里。您可以向其中添加错误捕获,但这可能是为了以后的课程:
numbers = [1, 2, 3]
num_words = ['One', 'Two', 'Three']
print('Type a number from 1 -3: ')
user = int(input())
if user in numbers:
print(f"You typed: {num_words[numbers.index(user)]}")
#output:
Type a number from 1 -3:
2
You typed: Two
因此我们要求用户输入一个数字并在列表中匹配该数字:
numbers = [1, 2, 3]
num_words = ['One', 'Two', 'Three']
print('Type a number from 1 -3: ')
user = int(input())
if user in numbers:
print(f"{user} is the digit you typed " + ???)
一旦完成,我想匹配在数字列表中找到的用户键入的内容,并在下一个列表中打印出相应的索引值。
所以用户输入 1 并检查数字,如果找到它,它会打印:一个 从 num_words 列表到用户。
我也试过字典,但无法弄清楚如何根据用户的输入和与字典键的匹配向用户显示匹配值:
nums = {1: 'One', 2: 'Two', 3: 'Three'}
print ('Type 1, 2, or 3: ')
user = int(input())
if user in nums.keys():
print(num.values) #Need it to print the nums.value based on the users input at match to nums.keys
else:
print('Goodbye!')
但是,我的 Google 搜索没有找到任何与我正在尝试做的类似的东西。我在这里找到了主题(例如:
从字典中获取值的语法是 dictionary_name[key]
。如果找不到密钥,这将引发 KeyError
,因此您可以使用 try/except
而不是提前检查并执行 if/else
:
nums = {1: 'One', 2: 'Two', 3: 'Three'}
print ('Type 1, 2, or 3: ')
user = int(input())
try:
print(f"{user} is the digit you typed {nums[user]}")
except KeyError:
print('Goodbye!')
如果您不希望此代码在无效整数输入上引发 ValueError
,您可以只使用字符串作为字典键:
nums = {'1': 'One', '2': 'Two', '3': 'Three'}
print ('Type 1, 2, or 3: ')
user = input()
对于列表的第一个示例,使用 .index()
查找第一个列表中数字的索引位置,然后使用该值查找第二个列表中的项目:
if user in numbers:
location = numbers.index(user)
print(num_words[location])
对于带有字典的第二个示例,只需使用标准字典查找:
if user in nums:
print(nums[user])
在这里。您可以向其中添加错误捕获,但这可能是为了以后的课程:
numbers = [1, 2, 3]
num_words = ['One', 'Two', 'Three']
print('Type a number from 1 -3: ')
user = int(input())
if user in numbers:
print(f"You typed: {num_words[numbers.index(user)]}")
#output:
Type a number from 1 -3:
2
You typed: Two