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
Mikkel Bielefeldt
3,227 PointsWhat's wrong with my code?
Why do I get an Error here?
name = input("What is your name? ") middle_name = input("What is your middle name, {}? If you don't have one {}, just hit enter! ".format(name)) last_name = input("What is your last name, {}? ".format(name)) full_name = name + middle_name + last_name
Traceback (most recent call last):
File "IfStatements.py", line 4, in <module>
middle_name = input("What is your middle name, {}? If you don't have one {}, just hit enter! ".format(name))
IndexError: tuple index out of range
1 Answer
Steven Parker
243,356 PointsIt looks like your template string (for "format") has two tokens to be replaced, but the "format" is only being given one argument. If you want the same argument to appear twice, include it in the arguments twice.
# original code
"What is your middle name, {}? If you don't have one {}, just hit enter! ".format(name)
# fix using two arguments
"What is your middle name, {}? If you don't have one {}, just hit enter! ".format(name, name)
# another fix would be use only one token
"What is your middle name, {}? If you don't have one, just hit enter! ".format(name)
And for future questions, make sure to use Markdown formatting for the code (as I did here). Instructions can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.