Printed items from LISt do not match what was inserted
<p>Sorry guys, a bit new to Python so please bear with me.</p>

<p>I am attempting, just for S and G's to create a small python program that will take X number of digits of Pi, and play them as winsound.beeps (don't ask).</p>

<p>I got the beeps to beep, and I got Pi inserted into a list. When i print the list, it's not correct. Can anyone point to what I may have done wrong?</p>

<pre><code>#!/usr/bin/python
from mpmath import *

import winsound

mp.dps = 10

floatPi = mp.pi
print(floatPi)

conPi = str(floatPi)
print(conPi)

strPi = conPi.replace(".", "")
print(strPi)

listPi = []

for digit in strPi:
    listPi.append(int(digit))

print listPi
#winsound.Beep(floatPi*100, 300)

for number in listPi:
    print(listPi[number])
    #winsound.Beep(listPi[number]*100, 300)
</code></pre>

<p>Results are as follows </p>

<pre><code>3.141592654
3.141592654
3141592654
[3, 1, 4, 1, 5, 9, 2, 6, 5, 4]
1
1
5
1
9
4
4
2
9
5
</code></pre>

<p>Why is the printed list from the for loop not Pi?</p>

<p>The code should be ,</p>

<pre><code>for number in listPi:
    print(number)
</code></pre>

<p>Cause here you are iterating through the contents of the list not the keys associated with them , for example take the first case where it is <code>print(listPi[number])</code> it essentially prints <code>lisPi[3]</code> which is <code>1</code>.</p>

<p>And if you are from c/c++/java , </p>

<pre><code>for i in range(0,len(listPi),1):
    print(listPi[i])
</code></pre>

<p>This will be more familliar.</p>

python list python-2.7