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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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']
>>>
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.
>>>
[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]