gpt4 book ai didi

python - 创建一个程序,将用户输入的字母添加到Python的字典中

转载 作者:行者123 更新时间:2023-12-01 01:31:15 25 4
gpt4 key购买 nike

我必须编写一个Python程序来创建空的字母字典。必须填充字典,以便当用户输入任何字母时,它将作为键(大写)和值(小写)附加到字典中。字典中的所有字母必须是唯一的,这意味着字典中不允许重复。例如:

Enter any letters (separated with space): A b C d a
{'A': 'a','B': 'b', 'C': 'c', 'D': 'd'}

所以,这是我的代码:

letter_dictionary = {}
letters = input().split(' ')
for i in letters:
if i.upper() not in letter_dictionary:
letter_dictionary[i.upper] = i.lower

print(letter_dictionary)

但问题是,该程序添加到字典中的不是字母,而是函数 lower() 和 upper(),正如我在 pythontutor.com 上看到的那样我可以用它做什么?

最佳答案

尝试:

letter_dictionary[i.upper()] = i.lower()

关于python - 创建一个程序,将用户输入的字母添加到Python的字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52836740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com