I have a question about a simple quiz which is removing a specific key from a dictionary with function. Input must be taken from user. For example:
myDict = {'a':1,'b':2,'c':3,'d':4}
keyToRemove = ‘a’
would remove the first element from the dictionary and return the result.
I have developed such a code, but when I type 'a' it doesn't work. When I type only a it is removed. Could you help me to solve this question?
myDict = {'a':1,'b':2,'c':3,'d':4}
def key_Remover(Dict):
x = input("Please enter dictionary key to remove: ")
del Dict[x]
return Dict
key_Remover(myDict)
print(myDict)
input
takes the user input as a string. If the user puts in a
and hits enter, the input string is 'a'
. If the user puts in 'a'
and hits enter, the input string is "'a'"
.
'a'
exists in your dictionary, "'a'"
does not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.