Storing user values in a Python list using a for loop
<p>This is my Code for Asking user to input the number of cities and should allow only that number of cities to enter
Requirement is to use For Loop</p>

<pre><code>global number_of_cities
global city
global li
global container
li = []
number_of_cities = int(raw_input("Enter Number of Cities --&gt;"))
for city in range(number_of_cities):
    city = (raw_input("Enter City Name --&gt;"))
    li = city
    print li[]
</code></pre>

<p>Assuming you want a list of the cities in <code>li</code> you can do the following:</p>

<pre><code>li = []
number_of_cities = int(raw_input("Enter Number of Cities --&gt;"))
for city in range(number_of_cities):
    li.append(raw_input("Enter City Name --&gt;"))
print(li)
</code></pre>

<p>No need for the <code>global</code> in front of the variables and no need to define the other variables at the beginning, only the empty list. </p>

<p>Actually this is a nice example for learning list comprehension, e.g. </p>

<pre><code>n = int(raw_input("Enter Number of Cities: "))
li = [raw_input('City Name: ') for city in range(n)]
print(li)
</code></pre>

<p>This gives:</p>

<pre><code>&gt;&gt;&gt; n = int(raw_input("Enter Number of Cities: "))
Enter Number of Cities: 4
&gt;&gt;&gt; li = [raw_input('City Name: ') for city in range(n)]
City Name: London
City Name: Paris
City Name: Dubai
City Name: Sidney
&gt;&gt;&gt; print(li)
['London', 'Paris', 'Dubai', 'Sidney']
</code></pre>

python