Session 5, #3: Numbers From Files


So far, we have used a program to read in colors from a file. Lines from a file are read in words or strings (sequences of letters or other characters). Even if the characters look like a number, we need to tell Python to store them as a number (the default is to keep the values as strings unless told otherwise).

The way to convert a string to a number is with the command, float():

	num = float(line)
		
where line is a line of text from the file. Python will convert it to a number and store it the variable num. The name float comes from "floating point numbers" or another way to describe real number (roughly, numbers with decimal points).


Challenge: Make a new file of distances. To create a file, click on the + button. Write one number per line and make sure to change the main program to open your new file.