kid wants to know more about color on the screen

D

Doug Mitchell

Dear Group,

My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.

He is getting a bit frustrated with color coding.
For example in Chapter 2 page 18 and 19 Mr. Dawson describes a more fancy
way of printing "Game Over" on the screen. According to *him*...

When I type the command: print "Program 'Game Over' 2.0"
print \

Instead of getting the second "print" to turn orange, like it's supposed to
when you type in a command, it just stays black. And when its time to
actually run the program, Python just prints out Program Game Over 2.0
instead of the fancy large text like that is shown in the book.

Later on he says that strings within quotes will be green as expected and
then all of a sudden on the next line it will stay black :(. And when he
does a run script there will be a syntax error as Python wont recognize this
'black' string but then on another occasion the 'black' will run ok :(.

I am sure my inquiries are quite vague but I am trying to piece together
some of his 'groans and grunts' :(.

Any suggestions or other info you need from him?

Thanks for your advice.

Jack
 
L

Lee Harr

Dear Group,

My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.

He is getting a bit frustrated with color coding.
For example in Chapter 2 page 18 and 19 Mr. Dawson describes a more fancy
way of printing "Game Over" on the screen. According to *him*...

When I type the command: print "Program 'Game Over' 2.0"
print \

Instead of getting the second "print" to turn orange, like it's supposed to
when you type in a command, it just stays black. And when its time to
actually run the program, Python just prints out Program Game Over 2.0
instead of the fancy large text like that is shown in the book.

Later on he says that strings within quotes will be green as expected and
then all of a sudden on the next line it will stay black :(. And when he
does a run script there will be a syntax error as Python wont recognize this
'black' string but then on another occasion the 'black' will run ok :(.

I am sure my inquiries are quite vague but I am trying to piece together
some of his 'groans and grunts' :(.

Any suggestions or other info you need from him?


It sounds like maybe you are using a "highlighting" text editor. That means
the text editor adds color depending on the syntax of the colored text.
For instance, if you have

print "Game Over"

in the editor, the word "print" might be blue because it is a python keyword
and the "Game Over" might be orange because it is a string.

It is also possible that the book is doing something similar and coloring
the different words to show more information about their purpose or meaning.

If you then run that program in the normal text terminal it would just print
out in whatever color the terminal happens to be (in my case green text on
a black background).

If you want to make the text a particular color, you would need to tell python
how to change the color, and that is going to take some more work.

You may want to join the tutor mailing list which is set up specifically to
help people who are brand new to python and/or programming.

http://mail.python.org/mailman/listinfo/tutor
 
T

Terry Reedy

Doug Mitchell said:
Dear Group,

My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.

Congratulations to both of you.
He is getting a bit frustrated with color coding.

This is a post-beginner subject that I think would best be skipped for now.
But I know, kids like color as much or even more than adults.
For example in Chapter 2 page 18 and 19 Mr. Dawson describes a more fancy
way of printing "Game Over" on the screen. According to *him*...

When I type the command: print "Program 'Game Over' 2.0"
print \

Instead of getting the second "print" to turn orange, like it's supposed to
when you type in a command, it just stays black. And when its time to
actually run the program, Python just prints out Program Game Over 2.0
instead of the fancy large text like that is shown in the book.

I've have never seen the book so I can only give comments similar to
Harr's.
Standard Python by itself knows nothing of colors and fonts. The print
statement sends characters to the the file stream called stdout (for
standard output) which usually goes the the terminal and appears in
whatever font and color the terminal is set to. So Dawson had to specify
something extra to get extra control.
Later on he says that strings within quotes will be green as expected and
then all of a sudden on the next line it will stay black :(. And when he
does a run script there will be a syntax error as Python wont recognize this
'black' string but then on another occasion the 'black' will run ok :(.

Some program editors do something extra to get different portions of the
program displayed in different colors so the programmer can pick them out m
ore easily. In the one 'he' (Dawson or your son?) used, string literals
are apparently green. String literals denoted by a ' ' pair or a " " pair
may not contain a newline (carriage return to you probably), which is to
say, cannot continue across more than one line. So, if one types:

text = "Something to display
when the monster dies"

the editor might turn 'Something to display' green, but if it knows about
Python, it will leave 'when the monster dies' black because it cannot be
part of the string on the previous line. The black rather than green says
that something is wrong, and when you try to execute the above, Python says
SyntaxError.

Terry J. Reedy
 
D

Dan Bishop

Doug Mitchell said:
Dear Group,

My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.

He is getting a bit frustrated with color coding. ....
Later on he says that strings within quotes will be green as expected and
then all of a sudden on the next line it will stay black :(. And when he
does a run script there will be a syntax error as Python wont recognize this
'black' string but then on another occasion the 'black' will run ok :(.

It sounds like you're using a syntax highlighting text editor, that
is, it displays code in different colors depending on whether it's a
keyword, a comment, a string literal, etc. This highlighting helps
make it easy to see syntax errors (code that's in the wrong color is a
syntax error), but it's not part of the *.py file itself, and
therefore doesn't affect the output of the program.

However, it is possible to print colored text in Python.

Under Linux, you can print "ANSI escape sequences" (Look it up on
Google) to your terminal. These are strings starting with the control
character '\x1B' that, when printed, change the color of the text, or
move the cursor, or even change the frequency of the beep you hear
when you print '\a' (you can use this to make Python play simple
music).

I don't know how to do it on Windows, but there might be something for
it in the msvcrt module.
 
J

JanC

(e-mail address removed) (Dan Bishop) schreef:
Under Linux, you can print "ANSI escape sequences" [...]
I don't know how to do it on Windows [...]

Just use an ANSI device drivers for DOS/Windows.

(try googling for "ansi.com", "ansi.sys", "nansi.com", ...)
 
S

Sean Berry

Doug Mitchell said:
Dear Group,

My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.

He is getting a bit frustrated with color coding.
For example in Chapter 2 page 18 and 19 Mr. Dawson describes a more fancy
way of printing "Game Over" on the screen. According to *him*...

When I type the command: print "Program 'Game Over' 2.0"
print \

Instead of getting the second "print" to turn orange, like it's supposed to
when you type in a command, it just stays black. And when its time to
actually run the program, Python just prints out Program Game Over 2.0
instead of the fancy large text like that is shown in the book.

Later on he says that strings within quotes will be green as expected and
then all of a sudden on the next line it will stay black :(. And when he
does a run script there will be a syntax error as Python wont recognize this
'black' string but then on another occasion the 'black' will run ok :(.

I am sure my inquiries are quite vague but I am trying to piece together
some of his 'groans and grunts' :(.

Any suggestions or other info you need from him?

Thanks for your advice.

Jack



As previously mentioned... the author is not referring to the color of the
output of the code, but rather the color of the code itself.

If you are using python on a Windows machine then the way to get the text to
hightlight is to save it as a *.py file... like test.py. Without the .py at
the end the text highlighting will not work.

Once it is saved as a .py file... the following types of rules will apply.

# this entire line is red because it is a comment

"this text is green becuase it is a string in quotes"

print "Some stuff" <-- "print" is orange because it is a python keyword.

def this: <-- "this" is blue becuase it is the name of a function


I hope this helps.
 
N

news

Hi Doug, I am also using this text. On the whole I find it very very good.
I am looking at the pages you mention and it looks like you have not added
the rest of the code after the print \ statement ... there is a whole bunch
of code in triple quotes that follows, when this is included you should get
the correct syntax highlighting. Actually I did this exercise only last week
with 2 year 7 classes.
Your problems are common and you will soon get over them, wait till page 22
when you see escape sequences! .. realise all these things become old hat
very quickly.
regards
Darren Payne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top