[Tutor] counter

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 8 Feb 2002 13:19:11 -0800 (PST)


On Fri, 8 Feb 2002, kirk Bailey wrote:

> OK, something simple. I want to write a counter for a webpage.
> 
> This means reading a file, adding one to the contents, displaying the
> result, and writing that new number to the file and saving it.
> 
> Opening and closing and reading and wrigting files I grok. But I
> understand python is rather odd about numbers, reading a text
> expression of a number and saving same.

Yes, when we read(), what we get back will be a string.  However, we can
coerse the string into an integer by using the int() function:

###
>>> number = "42"
>>> int(number)
>>> number
'42'
42
###

For symmetry, we also have access to an str() function that can bend
anything into a string.


Good luck to you!