【发布时间】:2014-06-10 07:39:49
【问题描述】:
我正在制作一个程序,您可以在其中将字母和符号配对以破解密码,我正在使用字典来存储所有配对。我正在尝试创建一个系统,您可以通过访问字典并选择一个键(当前是符号)来取消配对符号并找到它的值,然后可以使用.replace()
问题是我找不到合适的方法来允许用户从字典中选择单个键并将值放入一个变量中,然后该变量可用于撤消配对。我曾尝试使用 .get() 函数,但我不知道如何让用户选择单个键。有没有具体的功能或者可以用.get()完成吗
这是我的代码:
if del_question == "2":
print("These are your letter and symbol pairings")
print(dictionary)
if dict.get(dictionary):
print(letter)
else:
print('not found')
del_letter = input("Please input the symbol you wish to unpair: ")
if del_letter in words:
del_symbol = dict.get(dictionary)
words = words.replace(del_symbol, del_letter)
del dictionary[del_letter]
print(words)
else:
print(del_symbol, "was not found")
这是我收到的错误消息:
Traceback (most recent call last):
File "S:\Computer Scince\Controlled Assessment\Codebreaker.py", line 86, in <module>
print(del_symbol, "was not found")
NameError: name 'del_symbol' is not defined
【问题讨论】:
标签: python python-3.x dictionary key-value