# Python sample file for November 15, 2010, CS 151, ISU
# Prepared by Jeff Kinne

# This first line has JES load all the functions from the
# cs151functions.py file so we can use them in this file too.
# So we will have this as the first line in our Python files from
# now on.
#
# Make sure to do the following in the command area first, and select
# the folder that has cs151functions in it:
# >>> setLibPath(pickAFolder())
from cs151functions import *


# solutions to the in-class exercises from last time.

def appendToFile(filename, sNew):
  # first read in the contents of the file.  You can use readFromFile
  # that is in the cs151functions.py file, or use the file functions
  # directly (file = open(...), s = file.read(), file.close())
  s = readFromFile(filename)

  # now create the new contents of the file, it should be s then sNew
  s = s + sNew
  
  # now write that out to the file.  You can use the writeToFile function 
  # if you want.  Or use file = open(...), s=file.write(...), file.close()
  writeToFile(filename, s)

# here is another one to do.  write a function to write out the first
# 10 perfect squares, one on each line of a file.
def perfectSquares(filename):
  # open up a file with the open function
  file = open(filename, "wt")
  
  # for loop to go 1 up to 10
  #  inside of that, compute i*i, and write it to the file using file.write(...)
  #  you can convert a number to string using the str function, so str(i*i)
  for i in range(1, 10+1):
    s = str(i*i) + "\n"
    file.write(s)
  
  # then close the file.
  file.close()
  
  # after that in the command window, you can read the file to make sure it 
  # looks like what it should

# A very important skill is being able to look up
# information about functions to figure out how to use them.
#
# Here is one to look up.  There is a function called pow
# that computes powers, for example 2^16 or 5^8.  It is covered
# in the book in section 10.5.  Look it up and how to use it.