Fork me on GitHub

In-Class Exercises

by Olivia Dorsey

30 Sep 2013

Here is my result from the in-class exercises today, thanks in part to Leslie's guidance!

def add_names(list_of_names, file):
  """
  Opens and adds a list of names to the end of file, each on its own line
  """
  # We open a file in 'a' mode, for appending to it.
  names_file =open(file, 'a')

  # For each line in the list, we print that to the file.
  # This assumes one file per line. 
  for name in list_of_names:
    print >> names+files, name

  # Close the file so the changes are visible.
  names_file.close()

# Exercise: make new_names customizible:
new_names= input('Enter a list of names: ')

# Exercise: make the file name used here customizible:
new_file=input('Enter a file: ')
add_names(new_names, new_file)
Olivia is a senior BSIS student who is interested in web design/development and the digital humanities. Her hometown is Clayton, NC. Find Olivia Dorsey on Twitter, Github, and on the web.
comments powered by Disqus