[Tutor] Hello new to python and the mailing list, need some help
alan.gauld at blueyonder.co.uk
Sun May 30 12:49:53 EDT 2004
Hi Kevin, welcome to Python.
There are several points to mke, only one of which
actually resolves your problem :-)
> ...I want to write to a seperate line so that it will
> print on the screen like this:
> here is the code that I did:
> NAME = raw_input("What is your name? ")
> AGE = raw_input ("What is your age? ")
> SEX = raw_input ("What is your sex? ")
First, by convention all uppercase letters are usually
reserved for fixed values or constants. Its just a convention
but a pretty well respected one. A mnore normal convention
for variable names is to keep then lower case and capitalize
any secondary words within the name., like this:
It doesn't change how the code works but makes it easier
for other people to read your code.
> player = open("name.plr", 'w')
> player.write (NAME)
You are writing the data but the data has no newline at the end.
You need to add it. Unfortunately python files have a readline()
method but no writeline() - for reasons which have always puzzled me!
To add the newline we use the character sequence:
so to write to the file you need to do:
> player = open("name.plr", 'r')
> line = player.readline()
This will only read one line of input, if you need to read the
others you will either have to add multiple lines like the above
or use a loop - which you may not have come across yet in whatever
tutor you are following!
Author of the Learn to Program web tutor
More information about the Tutor