Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Murray
3,294 PointsTwo sets of curly brackets not working in lumberjack()
Hi, when I use two sets of braces (as shown in the video) for the string in the else: part of the lumberjack function, I get an error as shown below. If I remove a set of braces, the function runs ok. What is causing this please?
def hows_the_parrot():
print("He's pining for the fjords!")
hows_the_parrot()
def lumberjack(name):
if name.lower() == 'Kenneth':
print("Kenneths a lumberjack and he's ok")
else:
print("{} sleeps all night and {} works all day!".format(name))
lumberjack("Kenneth")
The error message:
Traceback (most recent call last):
File "functions.py", line 12, in <module>
lumberjack("Kenneth")
File "functions.py", line 10, in lumberjack
print("{} sleeps all night and {} works all day!".format(name))
IndexError: tuple index out of range
1 Answer
Valeriya Romashchenko
11,841 PointsIf you put two sets of braces in the string, it means you need to pass to arguments in the format function, otherwise, python doesn't know where to put your name.
print("{} sleeps all night and {} works all day!".format(name, name))
Also, you have code like
if name.lower() == 'Kenneth':
In your case, it should be:
if name.lower() == 'kenneth':
name.lower() will never be equal to the string that begins with the uppercase letter because you transform your name string into all lowercase letters.
Daniel Murray
3,294 PointsDaniel Murray
3,294 PointsThank you Valeriya for the quick reply :)
Valeriya Romashchenko
11,841 PointsValeriya Romashchenko
11,841 PointsSure! Have a good coding!