I'm wondering how to assign a a few different files to different corresponding numbers in python
<p>I'm wondering how a assign the file to just one of the numbers in the list, and if they make another file it will assign it to the next corresponding number.</p>

<pre><code>number = [1, 2, 3, 4, 5]

while True:
    with open(number, "w") as w:
        number.write(input(""))
    user_answer = input("1 to start another file, 2 to save and exit. -")
    if user_answer == ("1"):
        True
    elif user_answer == ("2"):
        break
</code></pre>

<p>Let's take your program line by line.</p>

<pre><code>number = [1, 2, 3, 4, 5]
</code></pre>

<p>The only problem here is that <code>number</code> is not quite the right name for your variable.</p>

<p><code>[1, 2, 3, 4, 5]</code> isn't a single number, but a list of five numbers. Choosing good names for variables is an important (and sometimes tricky) skill for a programmer, because if your names are confusing, well, you'll get confused. Let's call it <code>number_list</code> instead.</p>

<pre><code>number_list = [1, 2, 3, 4, 5]
</code></pre>

<p>Ok, what's next?</p>

<pre><code>while True:
</code></pre>

<p>This is the standard way to repeat forever (or until told to stop) in Python. I think what you really want to do is go through each of the numbers in <code>number_list</code> in turn, though, and for that, we use a <code>for</code> loop:</p>

<pre><code>for number in number_list:
</code></pre>

<p>On the next line you're trying to open the file for writing:</p>

<pre><code>    with open(number, "w") as w:
</code></pre>

<p>There's a problem here though, because <code>number</code> is an <a href="https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex" rel="nofollow">integer</a>, and filenames have to be <a href="https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str" rel="nofollow">strings</a>. We can fix that by converting <code>number</code> to a string using <code>str()</code>.</p>

<p>Also, <code>w</code> is a slightly confusing name for your file variable, because it's similar to the <code>"w"</code> you used to say that you're writing to the file â traditionally files are called <code>f</code> when we open them like this.</p>

<p>Let's make both those changes:</p>

<pre><code>    with open(str(number), "w") as f:
</code></pre>

<p>Next line:</p>

<pre><code>        number.write(input(""))
</code></pre>

<p>The problem here is that you're trying to write to your integer <code>number</code>, when you really want to write to the file you just opened, <code>f</code>. That's an easy fix though:</p>

<pre><code>        f.write(input(""))
</code></pre>

<p>Your next two lines are perfect:</p>

<pre><code>    user_answer = input("1 to start another file, 2 to save and exit. -")
    if user_answer == ("1"):
</code></pre>

<p>... but the one after isn't quite right:</p>

<pre><code>        True
</code></pre>

<p>The way we actually tell Python to go back to the top of the loop with the next value is like this:</p>

<pre><code>        continue
</code></pre>

<p>Your last two lines are also perfect:</p>

<pre><code>    elif user_answer == ("2"):
        break
</code></pre>

<p>Putting the changes together, we get:</p>

<pre><code>number_list = [1, 2, 3, 4, 5]

for number in number_list:
    with open(str(number), "w") as f:
        f.write(input(""))
    user_answer = input("1 to start another file, 2 to save and exit. -")
    if user_answer == ("1"):
        continue
    elif user_answer == ("2"):
        break
</code></pre>

<p>Quick question though: what happens if the user doesn't enter either '1' or '2'? Something to think about ...</p>

python python-3.x