【发布时间】:2021-03-04 23:17:59
【问题描述】:
所以我们要求用户输入一个数字并在列表中匹配该数字:
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 列表到用户。
我也尝试了字典,但不知道如何根据用户的输入和与 dict 键的匹配向用户显示匹配值:
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 搜索没有找到与我正在尝试执行的操作类似的任何内容。我在这里找到了主题(例如:Stackover Flow Tpoic、Stackover Flow topic 2,但这并没有帮助,因为据我所知(以及目前对 Python 的理解)它并没有 100% 符合我想要做的事情。
【问题讨论】:
-
将
print(num.values)更改为print(nums[user])
标签: python python-3.x list dictionary user-input