Anthony's first program-it's interesting to see a six-year-old programming python.

Source: Internet
Author: User

Http://davidbau.com/archives/2005/07/29/haaarg_world.html

Haw.g, world!

Continuing the "Anthony's first program" series from last time.

When my six-year-old son Anthony asked me if he cocould program his
Own computer game, I was worried that he might be totally disappointed.
It is not 1979 anymore. The gap between the primitive software you can
Put together in an afternoon and highly polished Multimedia Software
You can buy at the store for $9.99 isHuge.

In 2005, cocould programming hold the interest of a six-year-old
All? Does anybody even program their own computers anymore? And what
Tools wocould you use as a beginner? Which language? Which operating
System?

I am a professional programmer, but the tools I use seem far out
Reach of a beginner. I have no idea how a real novice shocould learn
Program in 2005. And so after dinnertime last Friday, when Anthony
Asked me for the 10th time to help him write a computer game, we wowould
Have to come up with something. It was time to get started ....

An operating system for vampires

For a six-year-old, the idea of writing a computer game was just
Like Arts and Crafts, costumes, and pretend. "Sam's sister wants
Make a pet show game, "Anthony explained to me," the person who makes
Their pet do the best tricks wins. "at six, you see no limits in
World. You can create anything you can imagine.

I had to explain to him that he wowould have to pick a simple game
With rules. making, for example, a "pet show" where you coshould tell your
Pet To Do Anything wocould be hard, because there were too then things
You wowould have to teach the computer about pets.

So we had to pick something like "tic-tac-toe", or "guess a number
From 1 to 100 "or that kind of thing. Anthony decided" guess a number"
Wocould be a great video game, especially if a vampire was the one that
Was keeping the super-secret number. He wocould have to have a really
Scary vampire.

Anthony wanted to get started on his vampire game right away. He
Didn't have time to watch me fool with installing cygwin or Python or
The j2se SDK. We had a couple hours. We wowould need to get something
Working before bedtime.

So before even choosing a language, I had to pick which computer
Use with Anthony: Windows or Mac. It really only took me a second
Decide. On Windows, I wocould have to install a bunch of Development
Tools-a language, an SDK, etc-and then I wowould have to get it all
Configured actually work.

But the Mac is Unix. It is a hacker's system. It comes with all
Important versions ages: C, Java, Perl, and python, not to mention bash and
Sed and awk. and VI. And Emacs. on the Mac, you can turn it on and go.
We cocould get started right away.

"Vampire" wocould be written for the Mac.

Hello-er-"haw.g," World!

So we opened up two OS X terminal windows and put them side by side.
One window (running Emacs) was for writing the program, and one window
(With a shell) was for running it, I explained. I created a file called
"Guessanumber", chmod 755, and then typed in an example program.

I picked the simplest, clearest language I cocould think of. Then I
Told Anthony, "This first line tells the computer what language we are
Using. It is called Python. in Python, you can print things by saying
Print ."

This is what I gave him:

 
 
#! /Usr/bin/ENV Python
Print "hello ."
 

And I showed him how to run it:

 
 
>./Guessanumber
Hello.
>
 

Anthony is a typical 6-year-old, and he does not keep his opinions
To himself. "That's no good! "He was disappointed." The Vampire shocould
Say 'haokg, I 've got the secret! 'It needs to be scarier! Can you make
It say that? "

But Anthony can type. Hunt-and-Peck is something first graders know
How to do in 2005. "Anthony, you can type it in if you like ."

So here is what he did. He was especially happy with his own all-caps innovation:

 
 
#! /Usr/bin/ENV Python
Print "haw.g! I 've got the secret"
Print "it's a number. 1-100"
 

And then:

 
 
>./Guessanumber
Haw.g! I 've got the secret
It's a number. 1-100
>
 

His vampire had printed out haaarg! A huge smile upload up Anthony's face.

We were off to the races.

Interacting with Python

I will spare you all the details, but I will show you a couple
Versions of the python program Anthony wrote. I find it interesting
See what a 6-year-old understands first, and what comes next.

Here was the first program that you cocould actually play:

 
 
#! /Usr/bin/ENV Python
Secret_number = 28
Print "haw.g! I 've got the secret"
Print "it's a number. 1-100"
Print "Guess! "
Guess = input ()
If guess = secret_number:
Print "you win! "
Else:
Print "I win! "
 
 
>./Guessanumber
Haw.g! I 've got the secret
It's a number. 1-100
Guess!
50
I win!
>
 

As soon as the program did this, Anthony said, "I want the game to be even harder! Let's make it one to a thousand! "

"But Anthony," I said, "Even if you do that, I can always win your
Game by typing 28. "He did not seem to be concerned about that at all.
But then he changed his mind and decided that it was more important
That the vampire needed to be better at taunting you.

Timing is everything

"I want it to wait two seconds before it says guess! Can we do that? "

The beauty of python is that it is a professional programming
Language, not just a plaything. The answer to 'can we do that? 'Is
Almost always 'yes', and 'it is well-doumented and discussed on
Internet. 'googling [Python sleep] gave Anthony a beautifully small piece of code to copy, with just two more lines.

 
 
#! /Usr/bin/ENV Python
Import time
Secret_number = 28
Print "haw.g! I 've got the secret"
Print "it's a number. 1-100"
Time. Sleep (2)
Print "Guess! "
Guess = input ()
If guess = secret_number:
Print "you win! "
Else:
Print "I win! "
 

As we watched and waited two seconds for "Guess! "To show up,
Anthony's face up again. "Guess! "He exclaimed, in his best scary
Vampire voice. And then he laughed. And then back to programming. "It
Shoshould wait a little longer! Maybe seven seconds, "as he started typing
Into Emacs again.

Timing is everything.

To be continued

The next step, of course, was to teach Anthony how to make a "while"
Loop so that you cocould guess a few times. And then after that it took
Little cajoling to convince him that the gameplay wocould be improved if
You gave hints like "try a bigger number" or "try a smaller number ".
Somehow we got through it all.

Before the end of the evening, we had a classic "Guess My number"
Game running. The two hours between dinnertime and bedtime had just
Barely been enough to get it written and working. We showed it off
Mom, who was suitably impressed, and then got into PJs and tucked
Bed.

In the next article, I will show you the code, and then tell you what happened the next morning.

Posted by David at July 29,200 5 am
| Trackback

Comments

We're re hiring.

Let me know when he learns Java.

Peace.

Posted by: Cameron at July 29,200 5 am

I wish my dad wowould have been like that. Great story!

Posted by: Michael at July 29,200 5 pm

I was about seven when I used the Internet to teach myself QBASIC on
An old Compaq desktop. My dad gave me the computer and the freedom
Learn how to use it. It wasn' t long before I wanted to do more
Just play other people's games. I totally understand your son's
Ambition, and I really appreciate what you're doing for him.

Posted by: Clay at July 29,200 5 pm

I hope I have kids like that one day !!!
Although I cant code... damn.
So when ure son wants to teach me how to write Python send him round!

Nogg3r5

Posted by: nogg3r5 at July 29,200 5 pm

This Anthony has to be just about the smartest kid in the whole wide world.

Love, Grandpa Paul

Posted by: Paul BAU at July 29,200 5 pm

Ah... You shoshould start his own blog... "as my childs game
Development continues... "It 'd be fun to watch him progress and make
More games.

Posted by: marble at July 29,200 5 pm

Awwww... That's sooooo cute! Makes me want to have kids !!

Posted by: djslim at July 29,200 pm

All my dad did was teach me FORTRAN.

Posted by: stupidideas at July 30,200 5 am

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.