Here is some code that prints out the value of a dictionary based on the key, I have tried some methods like.remove() but can't find a good solution that does not require an entire class to strip the dictionary.
Here is my code:
def WriteoutLines(TheDict, choice):
DictValues = TheDict.copy()
Result = TheDict.get(choice)
print(Result)
TheDict = {"pres": ["the President right now is",
"Joe Biden"],
"FSUNick": ["the seminoles",
"unconquerd"],
"class": ["introcution to Python"]
}
TheKeys = list(TheDict.keys())
Done = False
while (not Done):
print("we have these keys")
for i in range(0, len(TheKeys)):
print(TheKeys[i])
print("enter zz to end program or the word you want a listing for")
choice = input("enter the string you want typed out")
if (choice in TheDict.keys()):
WriteoutLines(TheDict, choice)
elif (choice == "zz"):
break
else:
print("The was not a legal choice")
Try str.join()
:
the_dict = {
"pres": ["the President right now is", "Joe Biden"],
"FSUNick": ["the seminoles", "unconquerd"],
"class": ["introcution to Python"]
}
while True:
print("we have these keys")
print("\n".join(the_dict.keys()))
print("enter zz to end program or the word you want a listing for")
choice = input("enter the string you want typed out")
if choice == "zz":
break
print(" ".join(the_dict.get(choice) or ["That was not a legal choice"]))
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.