posted Mar 14, 2018, 5:52 AM by Konstantinovich Samuel   [ updated Mar 14, 2018, 10:50 AM ]
Goal: Pie Pie Pie Pie Pie... actually just files.

Do not run this code, instead discuss what this actually does:

import random #used to make random numbers
sum,count = 0,0
while count <= 1000000:
x=random.random()#returns a value from 0.0 to 1.0 (excluding 1.0)
y=random.random()#returns a value from 0.0 to 1.0 (excluding 1.0)
if x**2 + y**2 <= 1:
print 4.0*sum/count

From now on: I will spot check notebooks in the middle of class and just penalize people who do not have the appropriate things written down. 
Simple rule: anything that says "on paper" or "write down" or "Draw" means it is required to be in your notes.

Main Idea:
Python can view the contents of a file using several commands.

New stuff:

escape character sequences 
'\n'  newline
'\t'  tab
'\\'  backslash

print "\\hi\\ \nhi"

1. Make a new file data.txt and paste in the following:

------------------copy everything after this line------------------
This is a text file.
There are things inside of it!
20 30 40 50
30.4 1.0 3.33 0.6668
------------------copy everything before this line------------------

2. In the same directory make a python file: loadfile.py

------------------copy everything after this line------------------
#Paste in this code, then test the 3 different read commands.
file = open("data.txt","r")   # r means read mode

#section 1
#text = file.read()

#section  2
#text = file.readlines()

#section 3
#text = file.readline() 
#print "and then..." 
#print text
#text = file.readline() 

#always print text
print text
------------------copy everything before this line------------------

3. Test the code above in IDLE, and write in your notes:
Write a description of how each read command differs from the rest. 




Compare your descriptions with your neighbors and make adjustments as needed.

4. Now in IDLE:
When you use readlines() on the file: you will notice that 2 of the list elements has a string of numbers.

Write some code that takes a string that is integers separated by spaces, and convert the strings to int values, then the values of those integers together.

Put that string (from your list of strings) in another variable to make it easier to play around with. 
It can be split up using either : split()   or split(" ")  -> look it up, you wrote something just like it!

First Goal:
Download the text file attached to this post. (mac/linux should NOT download the windows version!)
Find the sum of all of the values.(hint, look at the file, notice each number is on a new line!)

Second goal:
Try to add up the values. *hint: write a function!*

"20 30 40 50"   ->  140 (the integer)
"1 2 3 4" -> 10 (the integer)

How do you convert a string to an int or floating point number?

Final Goal Try to sum a file that looks like this: (put them in a text file!)

Konstantinovich Samuel,
Mar 14, 2018, 5:52 AM
Konstantinovich Samuel,
Mar 14, 2018, 5:52 AM