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

Python

"IndexError: tuple index out of range"

G'day, I seem to be having trouble running my script in the python console. It looks like this:

def hows_the_parrot():
    print("He's pining for the fjords!") 

hows_the_parrot() 

def lumberjack(name):
    if name.lower () == 'Saihej':
        print("Saihej's a lumberjack and he's OK!")
    else: 
        print("{} sleeps all night and {} works all day!".format(name))

lumberjack("Saihej")

and I receive an error stating this:

He's pining for the fjords! Traceback (most recent call last): File "functions.py" , line 12, in <module> lumberjack("Saihej") File "functions.py", line 10, in lumberjack print("{} sleeps all night and {} works all day!".format(name)) IndexError: tuple index out of range

Can anyone possibly help me with this? I've been receiving all kinds of different errors before, but this one pops up the most, I cannot figure out what I'm formatting properly. it's been bugging me for a few days now >:(, I'm certainly not too good at it yet haha.

Thanks!

1 Answer

I think the problem is that your final print statement has two {} placeholders, but only one variable passed to the format method. You'll need to either:

  • remove one of the {}s from your string.
  • pass two variables to format (you can pass the same variable twice if you want, just separate them with a comma.

Thanks, it worked!