pythondictionarymergeddictionaries

Python Dictionary output issues


I am new to python and this forum. Online learning isn't working for me so I can't just go to a tutor. It could be something minor I am forgetting. I welcome any assistance you can give me.

I am trying to make the output look like this: Her name is Emmylou; She is on track to graduate in Fall 2021; Her bill is paid; Her major is Archeology; She belongs to these school clubs–Photography, Acting and Glee

Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}

for Key, Value in Emm.items():

print(f"Her {Key} is {Value} and she is on track to {Key} in {Value}; Her {Key} is {Value}; Her {Key} is {Value}; She belongs to these school {Key} {Value}")

The output is a mess and looks like this when I run it:

Her name is Emmylou and she is on track to name in Emmylou; Her name is Emmylou; Her name is Emmylou; She belongs to these school name Emmylou
Her graduate is Fall 2021 and she is on track to graduate in Fall 2021; Her graduate is Fall 2021; Her graduate is Fall 2021; She belongs to these school graduate Fall 2021
Her bill is paid and she is on track to bill in paid; Her bill is paid; Her bill is paid; She belongs to these school bill paid
Her major is Archeology and she is on track to major in Archeology; Her major is Archeology; Her major is Archeology; She belongs to these school major Archeology
Her clubs- is Photography, Acting and Glee and she is on track to clubs- in Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; She belongs to these school clubs- Photography, Acting and Glee

Solution

  • Ass other people tell you, you are iterating over the dictionary and in each iteration the key and value are replaced and printed in a new line.

    If you want to print in a single line using the dictionary, you can try to convert the dictioray in an array and printed using the format method.

    Emm = {
        'name' : 'Emmylou',
        'graduate' : 'Fall 2021',
        'bill' : 'paid',
        'major' : 'Archeology',
        'clubs-' : 'Photography, Acting and Glee'
    }
    
    items = []
    for (key, value) in Emm.items():
        items = items + [key, value]
    print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*items))