Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

IndexError: tuple index out of range

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


invert314 at gmail

May 30, 2009, 1:11 PM

Post #1 of 3 (8685 views)
Permalink
IndexError: tuple index out of range

My simple python script gives me an error when I try to execute it. Here is
the error message:

daniel [at] ibe:~/Desktop/python$ python3 str_format2Daniel.py
> Traceback (most recent call last):
> File "str_format2Daniel.py", line 8, in <module>
> print ("{0} is {1} years old {3} and ".format(name, age, append))
> IndexError: tuple index out of range
> daniel [at] ibe:~/Desktop/python$
>

Here is my script:

#!/usr/bin/python
> # Filename: str_format2.py
>
> age = 23
> name = 'Daniel'
> append = '6 months'
>
> print ("{0} is {1} years old {3} and ".format(name, age, append))
> print ('{0} will be an elite python programmer in {2}.'.format(name,
> append))


What am I doing wrong?


python at mrabarnett

May 30, 2009, 1:33 PM

Post #2 of 3 (8626 views)
Permalink
Re: IndexError: tuple index out of range [In reply to]

Invert wrote:
> My simple python script gives me an error when I try to execute it.
> Here is the error message:
>
> daniel [at] ibe:~/Desktop/python$ python3 str_format2Daniel.py
> Traceback (most recent call last):
> File "str_format2Daniel.py", line 8, in <module>
> print ("{0} is {1} years old {3} and ".format(name, age, append))
> IndexError: tuple index out of range
> daniel [at] ibe:~/Desktop/python$
>
>
> Here is my script:
>
> #!/usr/bin/python
> # Filename: str_format2.py
>
> age = 23
> name = 'Daniel'
> append = '6 months'
>
> print ("{0} is {1} years old {3} and ".format(name, age, append))

That should be:

print ("{0} is {1} years old {2} and ".format(name, age, append))

> print ('{0} will be an elite python programmer in {2}.'.format(name,
> append))
>
>
> What am I doing wrong?
>


--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

May 30, 2009, 3:35 PM

Post #3 of 3 (8621 views)
Permalink
Re: IndexError: tuple index out of range [In reply to]

MRAB wrote:
> Invert wrote:
>> My simple python script gives me an error when I try to execute it.
>> Here is the error message:
>>
>> daniel [at] ibe:~/Desktop/python$ python3 str_format2Daniel.py
>> Traceback (most recent call last):
>> File "str_format2Daniel.py", line 8, in <module>
>> print ("{0} is {1} years old {3} and ".format(name, age, append))
>> IndexError: tuple index out of range
>> daniel [at] ibe:~/Desktop/python$
>>
>>
>> Here is my script:
>>
>> #!/usr/bin/python
>> # Filename: str_format2.py
>>
>> age = 23
>> name = 'Daniel'
>> append = '6 months'
>>
>> print ("{0} is {1} years old {3} and ".format(name, age, append))
>
> That should be:
>
> print ("{0} is {1} years old {2} and ".format(name, age, append))

Actually
print ("{0} is {1} years old and {2}".format(name, age, append))

>
>> print ('{0} will be an elite python programmer in {2}.'.format(name,
>> append))

And the latter should replace 2 with 1.

>> What am I doing wrong?

Using position indexes that are too large. In Py3.1, .format will
auto-number fields, so the print statements could be

print("{} is {} years old and {}".format(name, age, append))
print ('{} will be an elite python programmer in {}.'.format(name, append))

Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.