【发布时间】:2020-07-07 05:13:25
【问题描述】:
我无法继续使用 Python 练习的程序。这是关于 Python 中的字典。
问题是:编写一个python程序来检查给定的键是否存在,如果存在则打印值,否则添加一个新的键和值。
我的解决方案:
class Pro2:
def check(self):
dict = {}
a=""
b=""
c=""
d=""
for x in range(5):
a=(input("Enter key: "))
b=(input("Enter value: "))
dict[f"{a}":f"{b}"]
c=input("Enter a key which is to be checked: ")
if (dict.__contains__(c)):
print(dict[c])
else:
d=input("Enter the value to be added: ")
dict[f"{c}":f"{d}"]
现在出现的问题是,接受的输入没有附加到“for”循环中的相应字典中。 任何人都可以帮助我。更好的解决方案的建议也被接受。 提前谢谢你!
【问题讨论】:
标签: python dictionary for-loop user-input