title: python: ValueError: I/O operation on closed file
body: <p>My code:</p>
  
  <pre><code>with open('pass.txt') as f:
          credentials = dict([x.strip().split(':') for x in f.readlines()]) # Created a dictionary with username:password items
  
          name_input = input('Please Enter username: ')
  
          if name_input in credentials:  # Check if username is in the credentials dictionary
  
              name_input = input('Please Enter new username: ')
  
  
  
          f.write(name_input)
          f.write(":")
          pass_input = input('Please Enter password: ')
          f.write(pass_input)
          f.write("\n")
          f.close()
  
          print('Registered')
  </code></pre>
  
  <p>I am getting this error: </p>
  
  <pre><code>Traceback (most recent call last):
    File "silwon.py", line 146, in &lt;module&gt;
      f.write(name_input)
  ValueError: I/O operation on closed file.
  </code></pre>
  
  <p>also how to use sys.exit after the user enters the same username 3 times?</p>