FAQ: Exceptions - Review

This community-built FAQ covers the “Review” exercise from the lesson “Exceptions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Exercise Link: Learn Intermediate Python 3 | Codecademy

instrument_familes = {
  'Strings': ['Guitar', 'Banjo', 'Sitar'],
  'Percussion': ['Conga', 'Cymbal', 'Cajon'],
  'Woodwinds': ['Flute', 'Oboe', 'Clarinet']
}

def print_instrument_families():
  for family in ['Strings', 'Percussion', 'Woodwinds']:
    print('Some instruments in the ' + family + 'family are: ' + str((instrument_familes[family]))

instrument_variable = print_instrument_families()
print(instrument_variable)

The error message reads: File “families.py”, line 11
print_instrument_families()
^
SyntaxError: invalid syntax

My question is this, why can it not print the variable? I understand the exercise wants me to use try and except to get around it but I cannot figure out what syntax error exists to begin with.

You should be expecting the final print statement to output None.

>>> instrument_familes = {
...   'Strings': ['Guitar', 'Banjo', 'Sitar'],
...   'Percussion': ['Conga', 'Cymbal', 'Cajon'],
...   'Woodwinds': ['Flute', 'Oboe', 'Clarinet']
... }
>>> def print_instrument_families():
...   for family in ['Strings', 'Percussion', 'Woodwinds']:
...     print('Some instruments in the ' + family + 'family are: ' + str(instrument_familes[family]))
... 
...     
>>> instrument_variable = print_instrument_families()
Some instruments in the Stringsfamily are: ['Guitar', 'Banjo', 'Sitar']
Some instruments in the Percussionfamily are: ['Conga', 'Cymbal', 'Cajon']
Some instruments in the Woodwindsfamily are: ['Flute', 'Oboe', 'Clarinet']
>>> print(instrument_variable)
None
>>> 
1 Like
instrument_familes = {
  'Strings': ['Guitar', 'Banjo', 'Sitar'],
  'Percussion': ['Conga', 'Cymbal', 'Cajon'],
  'Woodwinds': ['Flute', 'Oboe', 'Clarinet']
}

def print_instrument_families():
  for family in ['Strings', 'Percussion', 'Woodwinds']:
    print('Some instruments in the ' + family + 'family are: ' + str(instrument_familes[family])

instrument_variable = print_instrument_families()
print(instrument_variable)

try: 
  print_instrument_families()
except: 
  print("Instrument Families Failed To Print!")
finally:
  print("Codecademy Forums is this the correct answer?")

I removed the extra set of paranthesis but still get an error.

Only the paren that I indicated is extraneous. The ones at the end belong to str() and print() respectively.

Aside

At the day and age, concatenation is a little outdated. I would recommend using an f-string.

>>> def print_instrument_families():
...   for family in ['Strings', 'Percussion', 'Woodwinds']:
...     print(f"Some instruments in the {family} family are: {instrument_familes[family]!r}")
... 
...     
>>> instrument_variable = print_instrument_families()
Some instruments in the Strings family are: ['Guitar', 'Banjo', 'Sitar']
Some instruments in the Percussion family are: ['Conga', 'Cymbal', 'Cajon']
Some instruments in the Woodwinds family are: ['Flute', 'Oboe', 'Clarinet']
>>> 

Of note:

{instrument_familes[family]!r}
                           ^^
                            \\
                       representation

!r is known as the conversion flag.

1 Like

You are the freaking best mtf! Thank you for always helping me! :slight_smile:

1 Like

Right on!

Python's F-String for String Interpolation and Formatting – Real Python

1 Like

i do not understand why you had to put a str before instrument_family

Because it is a list! Its not possible to display a listobject itself as far as I know. You have to form it into a string. But I would like to know, how we can use the .split() method to remove the komma ?

While it is possible to print a list object, we cannot concatenate a non-string type in a string expression. That is why it must first be cast to a str type.

Splitting a list that is cast to a string would involve removing a lot of extraneous characters. How about we join the strings in the list?

print('Some instruments in the ' + family + ' family are: ' + ', '.join(instrument_familes[family]) + '.')
>>> print_instrument_families()
Some instruments in the Strings family are: Guitar, Banjo, Sitar.
Some instruments in the Percussion family are: Conga, Cymbal, Cajon.
Some instruments in the Woodwinds family are: Flute, Oboe, Clarinet.
>>> 

Sorry I made a false description in english.

[quote]
Splitting a list that is cast to a string would involve removing a lot of extraneous characters. How about we join the strings in the list?[/quote]

Very nice! Thank you so much :slight_smile:

1 Like