-5

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")
Steve Jobs
  • 63
  • 1
  • 1
  • 7
  • 1
    What is the output and what do you want it to be? Also, please remove everything from your code that is not related to the problem. We call that a [mre]. Do not use `input()` in your code, because the result will then depend on what someone enters. – Thomas Weller Dec 01 '21 at 17:22
  • Why not just do TheDict.get(choice)[0] – shockwave Dec 01 '21 at 17:23

1 Answers1

0

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"]))
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • 1
    Instead of answering duplicates, I think it's best to flag a post as a duplicate rather than suggesting the same solution as the question that the post duplicates. Your solution is the same as [this one](https://stackoverflow.com/a/11178075/6273251). – Random Davis Dec 01 '21 at 17:24