Reading sequential user input to an array in python
<p>I am very new to python, so apologise in advance if this question is straightforward.</p>

<p>I am writing a script in python that needs to ask the user for input sequentially (the input will all be floats), and then it needs to write that input to a list. The rest of the code will then do stuff with the list.</p>

<p>This is my first attempt:</p>

<pre><code>num_array = list()

a1  = raw_input('Enter percentage of A (in decimal form): ')
a2 = raw_input('Enter percentage of B (in decimal form): ')
a3 = raw_input('Enter percentage of C (in decimal form): ')


li = ['a1', 'a2', 'a3']
for s in li:
num_array.append(float(s))
</code></pre>

<p>It all works except when we get down to reading the values into the array. Then I get the error:</p>

<pre><code> num_array.append(float(s))
 ValueError: invalid literal for float(): a1
</code></pre>

<p>I suspect that for a regular Python-coder the error is obvious, but this is literally the first time I'm using it :-) It's awesome, btw!</p>

<p>Any help would be much appreciated. As soon as it's running I will add things to deal with nonstandard inputs etc. </p>

<p>It should be:</p>

<pre><code>li = [a1, a2, a3]
for s in li:
    num_array.append(float(s))
</code></pre>

<p>You want to refer to <code>a1</code> the variable and not <code>"a1"</code> the string</p>

<p>Note you could combine everything into your loop:</p>

<pre><code>num_list = []
for c in ("A","B","C"):
    num_list.append(float(raw_input('Enter percentage of {} (in decimal form): '.format(c))))
</code></pre>

<p>This will raise a <code>ValueError</code> if <code>raw_input</code> is not a valid <code>float</code> though.</p>

python arrays variables input