您好我正在构建一个小项目,用户可以在该项目中输入新密钥和值。如果用户输入已经输入字典的键或值,告诉他们需要输入他们当前输入的新密钥或值,是否可以显示一条消息?
这是我目前正在使用的一些代码。
# dictionary with key:values.
reps = {'#':'A', '*':'M', '%':'N'}
### The users guess
print ("Now it's your turn to guess the rest of the letters!")
###Score
guesses = 0
### Update the dictionary with the users new guesses
while guesses < 10:
symbol = input('Please enter the symbol: ')
letter = input('Please enter the letter: ')
reps[symbol] = letter
### Re printing the updated list
for line in lines_words:
txt = replace_all(line, reps)
print (txt)
guesses + 1
答案 0 :(得分:1)
您可以使用in
测试现有的键和值:
if symbol in reps:
print("You already defined that symbol")
对于这些字母,您必须在字典中对所有值进行测试:
if letter in reps.values():
print("You already defined that letter")
你可以将两者结合起来:
if symbol in reps or letter in reps.values():
print("You already used either the symbol or the letter")