-2

trying to get the output:

Kayleigh:
True 100
True 80
True 67
True 100
True 89

from the dictionary I made:

kayleigh = {'name':'Kayleigh',
            'grades':[100,80,67,100,89],
            'attendance':['True','True','True','True','True']}

since the grades and attendance both have 5 items so I try to match them, but there's this error message saying that the format is not built in dictionary.


AttributeError Traceback (most recent call last) in ----> 1 print ("Kayleigh:",{},{}.format(kayleigh.get('attendance'),kayleigh.get('grades')))

AttributeError: 'dict' object has no attribute 'format'

WY G
  • 129
  • 10
  • 1
    Where is your faulty code? We can not fix what we do not see ... – Patrick Artner Feb 09 '19 at 08:23
  • The error is self-explainatory: `.format()` is not defined on `dict` - it is a defined in `str.format()` - there are a bazillion examples around showing how to print a dictionary - which ones did you research and what did you use? – Patrick Artner Feb 09 '19 at 08:25
  • See f.e. for complex formatting: [how-to-pretty-print-nested-dictionaries](https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries) or for much simpler needs: [how-to-print-a-dictionary-line-by-line-in-python](https://stackoverflow.com/questions/15785719/how-to-print-a-dictionary-line-by-line-in-python) – Patrick Artner Feb 09 '19 at 08:27
  • Please note that you have some time remaining before this post is being closed as unclear. Kindly post your code and explain where in the code you are stuck to avoid it. – Austin Feb 09 '19 at 08:34
  • `@WY G`, please show what you tried (code), it would be better to answer you before digging the hill. Are you using `format()` method defined on string. – hygull Feb 09 '19 at 08:49
  • Thank you. I just edited my post. I will keep in mind next time – WY G Feb 09 '19 at 18:03

2 Answers2

0

You can use this code to do your job:

    kayleigh = {'name':'Kayleigh',
            'grades':[100,80,67,100,89],
            'attendance':['True','True','True','True','True']}

print(kayleigh['name'],':')
for i in range (len (kayleigh['attendance'])):
    print(kayleigh['attendance'][i],kayleigh['grades'][i])
0

You can try like this (or for better view, you can check this notebook on Github.

# Defining a function which will do the task
def print_message(d):
    print(d["name"] + ':')

    for attendance, grade in zip(d["attendance"], d["grades"]):
        print(attendance, grade)

# Initialization
kayleigh = {'name':'Kayleigh',
            'grades':[100,80,67,100,89],
            'attendance':['True','True','True','True','True']}

# Making a call to te function by passing an appropriate dictionary 
print_message(kayleigh)

Output

Kayleigh:
True 100
True 80
True 67
True 100
True 89

If you want to try it from the terminal then here is how will you do.

>>> def print_message(d):
...     print(d["name"] + ':')
...     for attendance, grade in zip(d["attendance"], d["grades"]):
...         print(attendance, grade)
... 
>>> kayleigh = {'name':'Kayleigh',
...             'grades':[100,80,67,100,89],
...             'attendance':['True','True','True','True','True']}
>>> print_message(kayleigh)
Kayleigh:
True 100
True 80
True 67
True 100
True 89
>>>
hygull
  • 8,464
  • 2
  • 43
  • 52
  • Thank you! I am not very familiar with the zip() function. is it only applied to dictionary? – WY G Feb 09 '19 at 18:34
  • Yes, you are welcome `@WY G`. `zip()` is basically to iterate over 2 or more lists/tuples simultaneously. Have a look at https://www.journaldev.com/15891/python-zip-function for more details. – hygull Feb 09 '19 at 18:38