我只打印存储在字典中的相应缩写有一点问题
user = input("Enter a Abbreviation: ")
dictionary = {"ADSL": "Application Programming Interface",
"IDE": "Integrated Development Enviroment",
"SDK": "Software Development Kit",
"UI": "User Interface",
"UX": "User eXperience",
"OPP": "Object Oriented Programming"
}
for x in dictionary:
if user in x:
print(user + ":" + " " + dictionary[x])
elif x != dictionary:
print("Abbreviation not found") 这是我的输出
Enter a Abbreviation: UI
Abbreviation not found
Abbreviation not found
Abbreviation not found
UI: User Interface
Abbreviation not found
Abbreviation not found 我只需要输入缩写键值,不需要所有缩写没有找到输出,我希望这是有意义的。
发布于 2021-12-10 11:10:56
字典的优点是它是“索引的”,所以你不需要找到关键。您遵循的是数组方法,这是一个错误的方法。
user = input("Enter a Abbreviation: ")
dictionary = {"ADSL": "Application Programming Interface",
"IDE": "Integrated Development Enviroment",
"SDK": "Software Development Kit",
"UI": "User Interface",
"UX": "User eXperience",
"OPP": "Object Oriented Programming"
}
if user in dictionary:
print(user + ":" + " " + dictionary[user])
else:
print("Abbreviation not found")发布于 2021-12-10 11:12:19
也许只是这个:
user = input("Enter a Abbreviation: ")
dictionary = {"ADSL": "Application Programming Interface",
"IDE": "Integrated Development Enviroment",
"SDK": "Software Development Kit",
"UI": "User Interface",
"UX": "User eXperience",
"OPP": "Object Oriented Programming"
}
for x in dictionary:
if user in x:
print(user + ":" + " " + dictionary[x])
else:
continue发布于 2021-12-10 11:15:40
我试过这个密码,它有效..。看来你在检查所有的选项..。
user = input("Enter a Abbreviation: ")
dictionary = {"ADSL": "Application Programming Interface",
"IDE": "Integrated Development Enviroment",
"SDK": "Software Development Kit",
"UI": "User Interface",
"UX": "User eXperience",
"OPP": "Object Oriented Programming"
}
found = 0
for x in dictionary:
if user in x:
found = 1
print(user + ":" + " " + dictionary[x])
if found == 0:
print("Abbreviation not found")https://stackoverflow.com/questions/70303554
复制相似问题