Jan-09-2021, 11:02 PM
Hi guys, I need help to understand the following codes' logic please. I was trying to print out the keys and values of my dictionaries. My codes did print out the result I desired but I would like to add an empty line in-between each dictionary so I wrote a simple print('\n') located inside my FOR loop. It produced 2 new lines instead of 1. Anyone knows why this happened? Thank you so much for reading this. Greatly appreciated for your help! :)
person_one = {'first_name': 'kevin', 'last_name': 'hart', 'age': 15, 'city': 'kissimmee'} person_two = {'first_name': 'john', 'last_name': 'smith', 'age': 22, 'city': 'orlando'} person_three = {'first_name': 'kobe', 'last_name': 'bryant', 'age': 27, 'city': 'tampa'} people = [person_one, person_two, person_three] for person in people: for name, info in person.items(): if name != 'age': print(f"{name.title()}: {info.title()}") else: print(f"{name.title()}: {info}") print('\n') #why does this code here produce 2 new lines instead of 1? when I remove this code, no new line is outputted
Output:First_Name: Kevin
Last_Name: Hart
Age: 15
City: Kissimmee
First_Name: John
Last_Name: Smith
Age: 22
City: Orlando
First_Name: Kobe
Last_Name: Bryant
Age: 27
City: Tampa
[Finished in 0.1s]