The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Audrey,

When you open a Finder window, there should be a menu labelled "Go" at the top of the screen. Click that, and in the menu that appears click Go to Folder...

Then, a dialog box will appear with a text box. Type in the folder name that I gave you, and it will open up the folder where you need to put easygui.py.

/Library/Frameworks/Python.framework/Versions/2.7/ is a special folder on your system that contains the code and modules that your computer uses to run Python. You don't need to create it.

Carter
Could you try downloading easygui-0.96.zip from here: http://sourceforge.net/projects/easygui/files/0.96/easygui-0.96.zip/ , extracting it, and putting the easygui.py from the zip file into site-packages?

Carter
Audrey,

Could you try opening Finder, selecting Go->Go to Folder…, typing in “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages” (no quotes), and putting easygui.py in that folder?

Carter
What error message are you getting? Could you copy and paste it into a reply?
Sorry for not getting back to you sooner.

The hangman.ui file is in the Examples folder that gets copied to your hard drive when you run the Hello World installer. It's in the Hangman folder.

It's also in the "HelloWorld2_source_code.zip" file at this link:
http://www.manning.com/sande2/HelloWorld2_source_code.zip


- Warren Sande
Because this forum strips off whitespace, we can't see your indenting. Could you send us a screenshot of your code in IDLE? Or send us the code you're trying to run (your version). The .py file.

You can send it to cp4khelp (at) gmail (dot) com.

Thanks,
Warren Sande
Good question!

The reason that lines 7-10 return None is that the reverse() function reverses the list "in place", which means it reverses "a" but does not return anything.

-Warren and Carter
Noreen,

Thanks for pointing that out. You are correct that the labels on the decision tree figure aren't in the same order as the code and description on P 111-112.

The Ketchup and Mustard labels on the figure should be swapped.

Thanks,
Warren Sande
That looks like a separate issue. The file at that URL doesn't exist yet. Change the url in your program to http://www.helloworldbook.com/data/message.txt and it should work.

Carter
I've never tried combining Pygame and PyQT, so I'm not sure what would happen. There's a good chance that you would encounter the same problems with the two event loops conflicting with one another.
Mixing Pygame and EasyGui is tricky and a little unpredictable, as you have found out. Each has it's own event loop, and the two event loops don't always co-exist nicely.

I'm afraid we can't offer much help on this one.

- Warren Sande
You are correct that numpy is what you need for working with numeric arrays. I haven't worked with numpy myself, so I can't give you much guidance on it. We don't cover it in our book because it is a more advanced topic.

- Warren Sande
Great question!

The reason the non-string version isn't working is due to the way choicebox() works.

In the string version, triplets is a list of strings. Each item is a string.
In the non-string version, triplets is a list of lists. Each item is a list of 3 strings.

So in the non-string version, when you try to remove an item from triplets, you need to be removing a list.

But the choicebox() function returns a string, not a list.

So, you are tring to remove, for example "['red', 'red', 'black']". But what you should be trying to remove is ['red', 'red', 'black'] (no outer quotes)

One clue to this is that this line of code works in the non-string version:

second_guess = choicebox("Pick a triplet: first guess is "+(first_guess),choices = triplets)

If first_guess was not a string, you wouldn't be able to concatenate it with the other string.

Although the choices that you pass to choicebox() are lists in this case, the choicebox() function "stringifies" them, and returns your choice as a string. That's what is messing up the remove().

Also, while the append() in the last line is working, it's not doing what you probably expect. It is actually appending a string to your list of lists. So after the last line, your list would look like this, if the remove() worked:

>>> triplets
[['red', 'red', 'red'],
['red', 'black', 'red'],
['red', 'black', 'black'],
['black', 'red', 'red'],
['black', 'red', 'black'],
['black', 'black', 'red'],
['black', 'black', 'black']
"['red', 'red', 'black']"]

If you really wanted to make the non-string version work, you would have to take the string that choicebox() returns, parse it out, and reconstruct the list. Essentially, transmogrify
"['red', 'red', 'black']" into ['red', 'red', 'black'] Then remove() that list from triplets.

I hope this helps.

- Warren Sande
Sorry for not replying sooner.

If you are using Windows, look for "Python 2.7" item in the start menu. Under that, one of the items is "IDLE (Python GUI)" That where you start IDLE.

If you're not using Windows, or if you are still having trouble, you can post another message here, or send an e-mail to our help e-mail address. You can find the help e-mail here:
http://helloworldbookblog.com/about-us/

- Warren Sande
The keyword 'in' should be orange. The fact that it's not orange in the book is just a typesetting error.

We don't get any errors with that code. Can you tell us exactly what the error message says?


- Warren Sande
You mean joshua_circle.py ?

We really need to see your actual code and the entire error message to figure out what's going on. Is there a way you can send us a screenshot?

You could try this. In the Idle interactive shell, just type
>>> import pygame

and see what you get.

- Warren
Could you copy-paste the error message into a forum post?

Carter
What did you call your program? That is, what's the filename?


- Warren
We now have a blog post that tells you what you need to install, if not using our installer.

http://helloworldbookblog.com/hello-world-second-edition-installation/


- Warren
What operating system are you using?

- Warren Sande
The 2nd Edition is now available directly from Manning and it is also In Stock on Amazon.

Main differences are:
- Printed in full color (including syntax-colored code listings)
- Uses PyQt instead of PythonCard
- Adds a section on Python Dictionaries
- Adds a chapter on details of how the Skier program works
- Adds a chapter where you write AI bots to compete in PythonBattle
- Adds more details on the differences between Python 2 and Python 3. (The examples still use Python 2.)
Sorry you're having trouble. The forum for Hello World has moved to a new site.
http://bit.ly/sszWCD
If you post there, you get quicker answers.

If you are running Windows, the download for the Python installer that comes with Hello World is here:
http://www.manning.com/sande/installers/hello_world_setup.exe

This can be found on the www.helloworldbook.com, under Resources. The link says "Windows Installer (Python, modules, and examples)".

Clicking on this link will download the installer to your
hard drive. The file is called "hello_world_setup.exe"

If you run this exe file, it will install Python 2.5.1, as well as some other modules that are used in the examples in the book.

There is also a YouTube video that shows how to run the installer. http://www.youtube.com/watch?v=mtfRUPspevA

If you are still having problems, write us at cp4khelp@gmail.com.


Warren Sande
Most likely, you don't have the image files, or you don't have them in the same folder as the code.

By the way, in case you didn't notice the messages to that effect, this forum has moved. We don't actively monitor this one anymore.


- Warren
In case you missed the post, out forum has moved. We're not using the Manning forums any more.

- Warren
> (updating so it goes to the top of the message list)
(updating so it goes to the top of the message list)
(updating so it goes to the top of the message list)
(reposting to bump it to the top)
We have moved the Author Forum for "Hello World! Computer Programming for Kids and Other Beginners" to a new site.

The new site is here: http://bit.ly/sszWCD

We did this for a couple of reasons:

1. The Manning forum strips whitespace, which makes it impossible to post Python code. The new forum works much better for this.

2. We were starting to get a lot of spam on this forum. It will be much easier to control that on the new forum.

We will not be responding to new posts on this forum as of Dec 1, 2011. Please use the new forum: http://bit.ly/sszWCD

Warren and Carter Sande
Just a reminder that the forum has moved.
We have moved the Author Forum for "Hello World! Computer Programming for Kids and Other Beginners" to a new site.

The new site is here: http://bit.ly/sszWCD

We did this for a couple of reasons:

1. The Manning forum strips whitespace, which makes it impossible to post Python code. The new forum works much better for this.

2. We were starting to get a lot of spam on this forum. It will be much easier to control that on the new forum.

We will not be responding to new posts on this forum as of Dec 1, 2011. Please use the new forum: http://bit.ly/sszWCD

Warren and Carter Sande
(updating so it goes to the top of the message list)
(updating so it goes to the top of the message list)
Just a reminder that the forum has moved.
(reposting to bump it to the top of the list)
Mr. Man,

Glad you're enjoying the book!

I suspect a typo in your program, most likely something with indenting. Please carefully check the indenting, and try it again. If that doesn't work, send your code in an e-mail attachment to cp4khelp@yahoo.com, and we'll have a look.

(Unfortunately, this forum doesn't work for posting code, as it strips off the indentation. We will soon be moving the forum to a new site with better functionality.)


- Warren Sande
Specifically, tabs and spaces at the start of each line.


- Warren Sande
That's cool!

Thanks for letting us know.


- Warren and Carter
I'm sorry to hear you're having frustrations. That's certainly no way to start!

My best guess is that you need Admin privileges to install Python correctly. (That may also be why it won't uninstall properly, either.)

This is a common problem with a lot of software on Windows.

If you can log in as Administrator (or a user account that has administrator privileges), it should work. Let us know.


Warren and Carter Sande
Python modules are usually stored in
C:/Python25/lib/site-packages/
(in Windows)
Some modules will only work with a certain version of Python. So, if you are using Python 2.5, and you have a set of modules that works with that, upgrading to 2.7 may cause some of them to stop working.

If you want to switch to 2.7, you'll have to get the Python 2.7 version of each of the modules you are using.

Your programs written in Python 2.5 should work in Python 2.7, since the changes are pretty minor.

Hope this helps.


- Warren Sande
Good One! You used the same trick we showed in Chapter 1:

print "hello" * 20

Nice to see you remembered that and made the connection to this problem.


Regards,
Warren Sande
I'm not sure it's possible to dynamically change the font size in a TextArea. I'll do some digging and get back to you.

By the way, the weird list with the {} is a Python "dictionary" We didn't cover dictionaries in the book, but it's another way of collecting things together (like a list), except you store pairs of items. Each pair is called a "key-value pair" and you use the keys to find values.

I'm working on a blog post about dictionaries, but if you Google "python dictionary" I'm sure you'll find lots of information about them.

Regards,
Warren Sande
As you might have gleaned from the error message, 'font' is not a dictionary, but rather an object. It happens that the font's parameters are stored in a dictionary, but you cannot change them by directly modifying the dictionary. There is, however, a method for changing the font size.

Here's a complete (although simplified) Pythoncard program that will do what you want. The .rsrc.py file is not included, but it has just two components, the TextArea and the Slider
(I'll use dots to indicate the leading whitespace)


from PythonCard import model
class MainWindow(model.Background):
....def on_Slider1_mouseUp(self, event):
........sliderValue = self.components.Slider1.value
........font = self.components.TextArea1.GetFont()
........font.SetPointSize(sliderValue)
........self.components.TextArea1.SetFont(font)
........event.Skip()

app = model.Application(MainWindow)
app.MainLoop()


Regards,
Warren Sande
On Windows, shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts.

So, you could make an IDLE shortcut on each child's desktop, and set the "Start In" field to their personal directory. (Once you make the desktop shortcut, right-click it, got to "Properties", then "General".)

I tried that with IDLE on Vista and Win 7, and id worked for me.

I'm not sure the same trick will work for SPE, which we use later in the book, but you could try it. SPE seems to remember the last place you saved a file, and defaults to that location for the next Save. However, since there is only one instance of SPE installed, it might only remember one "location", not one per user. So that's probably not what you want. They may have to just browse to their own folder. But at least you only have to do that the first time you save each program.

Hope you and your children have fun with the book this summer!

Regards,
Warren Sande
Yes, that's the trouble with putting web links in a printed book.

The link was valid when the book was published. www.pylogo.org used to be the url of the Python Logo interpreter. (A quick Google search and a look at some of the Cached pages confirms this.)

They have since given up that domain (which now appears to be some sort of Japanese cosmetics site), and created a Sourceforge page, for which you kindly provided the link.

We will correct this for future printings of the book and future releases of the e-book.


Thanks,
Warren Sande
It's not so obvious in this forum due to the fact that it strips whitespace, so we can't see the indenting, but...

The first 'print' you pointed to starts a new line of stars. (The comma at the end of the previous 'print' in the loop will keep printing stars on the same line).

The second 'print' you pointed to puts a blank line between the blocks of stars.


numBlocks = int(raw_input('How many blocks of stars do you want? '))
for block in range(1, numBlocks + 1):
for line in range(1, block * 2):
for star in range(1, (block + line) *2):
print '*', # print another star on the same line
print <----- # start a new line of stars
print <------ # start a new block of stars (insert a blank line)

Hope this helps.


Regards,
Warren Sande
Okay, it makes sense. Two more questions...

1. Any error messages, either when you run the program or when you close the window?

2. Did you type the code in yourself? Or run the code from the 'examples' folder (or the web site)?

If you typed it in, would you mind sending a copy to cp4khelp@yahoo.com ?

(This forum strips whitespace, so posting it here doesn't work.)


Regards,
Warren
Yes, the black window is the "console" window. Every Pygame program will have a console window along with the graphics window. This is normal.

Glad you got it working!


Regards,
Warren
dsloan,

Glad you're enjoying the book. Sorry to hear about your troubles in Ch 16.

First, what operating system are you running?

Second, I don't quite understand the statement: "Now I can close it. But it stays black."

If you closed the window, what is black? Can you send a screen shot?

Are you getting any error messages in the SPE or IDLE console?


Warren Sande
Actually, one more question...

3. Did you run the Hello World installer, or install Python (and Pygame) from some other source? If you didn't run our installer, what version of Python are you running?


Warren
Cornelia,

You are probably better off to run the Windows installer from our website:
http://www.manning.com/sande/installers/hello_world_setup.exe

This installs Python, Pygame, and everything else you need for the book.

The CD should contain the same installer, but as Carter said, we weren't involved with that.

Try the installer from the web site and let us know if that works. You should uninstall your current version of Python first.


Regards,
Warren Sande
If you use our installer, SPE isn't in the same StartMenu group as Python. It has it's own StartMenu entry, "Stani's Python Editor", with 3 items:
- SPE on the web
- Stani's Python editor (this is the one you want)
- Uninstall SPE

The SPE files should be in Program Files. On my machine, they are at:
Csmilierogram FilesSPE\_spe


- Warren Sande
Tim,

From what you described so far, it sounds like EasyGui is installed properly.

When you do "import easygui", it shouldn't do anything (except give you another prompt):

>>> import easygui
>>>

Importing is just making easygui available for Python to use. The fact that it didn't do anything is actually a good sign. If easygui wasn't installed properly or Python couldn't find it, you would get an error message.

To actually use easygui, you have to do something like:

easygui.msgbox("Hello there!")

If that doesn't work, let us know and we'll see if we can figure out what's wrong.

Regards,
Warren and Carter
Did you use the HelloWorld installer? It installs everything you need for the book, including EasyGui.

If you didn't, try uninstalling whatever version of Python you have and run our installer. You can get it here:

http://www.manning.com/sande/installers/hello_world_setup.exe


- Warren Sande
Kyle,

You were truing to use a variable called 'round'? Yes, that is the name of one of Python's built-in functions, which rounds off a float to the nearest integer.

So, you changed the variable name to 'rounddd'? That should work fine. The reason you're getting this "strange" result is that integers with a leading '0' are interpreted as octal numbers (base smilie in Python.

>>> n = 015202
>>> print n
6786

>>> n = 15202
>>> print n
15202

The leading zero in the first case makes Python think you are trying to set the value of n in octal, not decimal. If you want the decimal value, there are a few things you can do:

a) remove the leading zero

b) do this:
>>> n = int('015202',10)
>>> print n
15202

Hope this helps,


Regards,
Warren Sande
Our code (and images) are distributed under the MIT license, which allows you to:
" use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, ... subject to the following conditions:

The <original> copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software."

We include the permission notice by including a link to the MIT License web site in the header comments.

You should leave those original copyright comments there, and then add your own underneath.

There is no problem posting the code or images on your web site.

Your header comments would look something like this:


# kyles_skier.py Copyright Kyle, 2011
#
# Based on skier.py, Copyright Warren Sande, 2009
# Released under MIT license http://www.opensource.org/licenses/mit-license.php
#
# Modified by Kyle. kyles_skier.py is also released under the MIT license.
I'm sure we can help you get EasyGui working.

Fist, did you run the Hello World! installer for Mac? Or did you install Python some other way? What version of Mac OSX do you have?

If you ran our installer, then Python should be installed in this location:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/

You should put easygui.py in the sub-folder called "site-packages":
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/

If you put EasyGui.py in that folder, it should work.

If you already did that, and it's still not working, post another message and we'll figure out what's wrong.

Regards,
Warren and Carter
Yes, that problem is specific to Python 3 - it does not happen with Python 2.x. This is one of the more noticeable changes with Python 3.

In Python 2.x, we have input(), which returns a number ( * see further explanation below), and raw_input() which returns a string.

In Python 3, there is no more raw_input(), but Python 3's input() works like Python 2.x raw_input(); that is, it returns a string.

There is no comparable function in Python 3 to the Python 2 input() function - nothing that ask for input from the user and directly returns a number.

Your modification below is the best solution for the Number Guess program, where you are expecting an integer.

Regards,
Warren and Carter

-------------------
* further explanation

Actually, input() returns an evaluation of whatever is typed in. If a number is typed in, it returns the number. If a string in quotes is typed in, it returns the string. If a variable name is typed in, it returns the value of the variable. If a Python statement is typed in, it returns whatever that statement would return.

This actually makes input() rather "unsecure". For example, in the NumberGuess game, you can cheat by typing in "secret" when it asks for your guess. Since secret is the number you're trying to guess, it will tell you that you've won!

The eval() function (which is in both Python 2.x and Python 3.x) does the same thing, except it doesn't get the input from the console, it evaluates whatever is passed to it.
We can help with that.

But, unfortunately, this forum strips off whitespace, so we can't see what your indenting looks like.

Please send the code you have to cp4khelp@yahoo.com and we'll be glad to help.


Warren and Carter
Thanks for the comments about the book. Glad you're enjoying it.

We do mention indenting on Page 12, when we show the very first code that needs indents (Listing 1.2, the Number Guess game).

Then, there's no more code that needs indenting until the second version of the Number Guess game (the one that uses EasyGui) on Page 60, which is at the end of Chapter 6. Then, early in Chapter 7, we talk about indenting, starting on Page 65. That's when we really start using indented code, for IF blocks.

But perhaps we could make a more prominent note about it earlier on, as you suggest.

Thanks for the feedback!


Regards,
Warren Sande
Ashwin,

Thanks for the kind comments about our book.

We can't really look at your code properly, since this forum strips out the whitespace.

If you could send it as an attachment to cp4khelp@yahoo.com, we'll try to have a look at it.


Regards,
Warren Sande
Eric,

We had a look at the code you sent. There is a small typo. On line 107:

map_postition = map_position - 1280

(You have an extra 't' in the first one.)

If you fix that, it should work fine.


Regards,
Warren Sande
You might want to carefully check the Skier code you typed in. If the downloaded code runs fine, there must be some difference. Especially pay attention to indenting.

Indenting could also be the problem with the stars program.

SPE is installed by our installer. If you look in the Start Menu (assuming you are running Windows) you will see an entry for "Stani's Python Editor". The actual location of the file (in Windows) is: "Csmilierogram FilesSPE\_speSPE.py"

If you re-check your code and can't find the problem, send it as an attachment to cp4khelp@yahoo.com and we'll have a look at it.

Regards,
Warren Sande
Glad you're enjoying the book. We have posted answers to your questions.

Let us know if you need more help!


Regards,
Warren Sande
The skier images are avaliable in the downloads section of the book's web site, here:

http://www.manning.com/sande/sourcecode/All_Files_By_Chapter/hw_ch10_code/

I'm not sure what you mean by "not understanding this". What is the actual error message you are getting?

If you send your actual code as an attachment to cp4khelp@yahoo.com, we will have a look and try to figure out what's wrong.


Regards,
Warren Sande
All the answers to the self-test questions are in the back of the book.

For the multiplication table (Chapter 8, Question 3), the answer is on page 375.


Regards,
Warren Sande
All the answers to the self-test questions are in the back of the book.

For the multiplication table (Chapter 8, Question 3), the answer is on page 375.


Regards,
Warren Sande
Could you tell us what you mean by "not working" What exactly is happening? What error message are you getting (if any)?

We have a Mac running Snow Leopard and Python 2.5, and the only problem we experience is that the EasyGui window sometimes pops up behind other windows, so you may have to use Exposé (or shuffle windows around) to see it.


Warren and Carter
Since you're running Python 2.6, I guess you didn't use the Ubuntu installer we provided. (Our installer installs 2.5). That being the case, how did you install Easygui? It has to be in the right place for Python to find it.

It might be simpler to uninstall Python 2.6 and run our installer. Then you would have everything you need for the examples in the book.

You can download our Ubuntu installer here:
http://www.manning.com/sande/Installation_Instructions.html#linux


Regards,
Warren Sande
Glad you found a solution!

So, it looks like you don't have to do anything special. It appears the pickle module already knows how to handle UTF-8.


Warren Sande
Glad your son is enjoying the book!

It looks like in line 33 you have:

pygame.sprite.Sprite__init__(self)

It should be

pygame.sprite.Sprite.__init__(self)

(There's a dot after "Sprite", before "__init__")

Once you fix this, if you have other errors, check very carefully for typos and especially indenting. If you're really stuck, post another message here, and we'll be happy to help.


Regards,
Warren Sande
Try this. Add the line:

import codecs

at the start of the program.

Then, in the on_initialize() function, change line

f=open("words.txt", 'r')

to

f = codecs.open("words.txt", encoding="utf-8")

You have to make sure the words.txt file is saved with UTF-8 encoding.

As far as the German version of the book, I don't think that was fixed. We (the authors) weren't even aware of the German translation until after it was published, so we had no opportunity to check or fix those sorts of things.


Warren Sande
Regarding the installation, did you try the HelloWorld Linux installer?

http://www.manning.com/sande/installers/hello_world_ubuntu.tar.gz

I think the problems you listed are just how Pythoncard looks in Ubuntu. I don't know if there's anything you can do about it.


Warren and Carter
We are looking into this. Sorry we don't have an answer for you right now, but we're working on it.

Regards,
Warren Sande
Benjamin,

It's pretty difficult to debug these things, since there are so many things that could be wrong.

You might try uninstalling and re-installing PythonCard. Also make sure you have wxPython installed, and make sure that you have the right versions of wx and PythonCard to go with the version of Python you have installed.

If none of that works, post again and include the error messages you are getting.

Regards,
Warren Sande
Did you try the HelloWorld Linux installer?

http://www.manning.com/sande/installers/hello_world_ubuntu.tar.gz


Warren Sande
Unfortunately, this forum deletes whitespace, so I can't see if there are indenting errors. If you send the code to cp4khelp@yahoo.com, I'll have a look. (Send the .py file as an attachment.)

Regards,
Warren Sande
We never got to the root cause of the problem. But the other user solved the problem by uninstalling Python (deleting the whole Python25 folder) and re-installing using the book's installer:

You can find the installer here:
http://www.manning.com/sande/Installation_Instructions.html

Regards,
Warren Sande
Okay. A couple of questions. First, what operating system are you using (MacOS, Windows, etc.)?

Second, can you send me a screen shot of SPE with your code and the error message?

You can send it to cp4khelp@yahoo.com


Warren Sande
The window at the bottom of SPE can be many things, depending what tab you select. It sounds like you were using the "Shell" tab. The shell doesn't work correctly in all versions of SPE, so we don't use the SPE Shell in the book.

You want to use the top widow, which is the editor. That's where you type in code that you will save to a file. Then you run it by using the menu item "Tools > Run without Arguments."

In the code you attached, there is a typo in the second line. There is no quote at the start of "Hello There!", and there needs to be one. That's what gave you the "syntax error". If you fix that and it still doesn't work, let me know, and we'll figure out what's wrong.

Regards,
Warren Sande
Okay, could you send me the file
"csmilieython25site-packageseasygui.py" ?

I want to see what version you have and if there is something wrong with the file.

Thanks,
Warren Sande
Hi,

Can you post the exact code that he is trying to run? (That's the file that he has open in SPE when he gets the error.) Just reply to this post and paste it in.


Regards,
Warren Sande
That would be a fun project, but very challenging. I play Cribbage myself, but I wouldn't know how to write an algorithm for how to figure out what to keep and what to put in the crib, or what cards to play when pegging. Even counting up the score at the end is fairly complicated.

The book has an example of a card game (Crazy Eights), which is a game where the rules are pretty simple. If you decide to pursue the Cribbage game, you could look at that for some examples of creating and managing hands, etc. But the actual cribbage rules and especially the strategy would be quite complicated.

Sorry I can't be more help. If you decide to tackle it, I wish you luck.

Regards,
Warren Sande
The "while True" loop is just a way to make an infinite loop - one that runs forever (or until the user does something to end the program).

Remember that while loops keep looping while some condition is True. So, this basically says, "while True is True, keep looping". Since True is always True, it loops forever.

A while loop that doesn't loop forever might look like:
while i < 10:

As long as the comparison i < 10 is True, it will keep looping, but if i reaches 10, then that comparison will be False, and the loop will stop.

In the code in Chapters 18 and 19, you will also see "while 1:" That's just another way of making an infinite loop. It really means "while 1 != 0". Since 1 is always "not equal" to 0, the expression 1 != 0 is always true, so it loops forever.

To break out of the infinite while loop, we either need to use "break" or "continue", or end the program. You can end a Pygame program by closing the Pygame window, or by using the sys.exit() function, which is what we do.
So the while loop keeps running as long as the Pygame window is open.

The "done" variable tells the program whether the player is still playing the game, or if they are done (out of lives). Being done doesn't mean the program stops. The pygame window can still stay open after you lose you last life - you're just not playing any more. Done gets set to True when there are no more lives left (lives == 0).

So, when done is True, it means keep the Pygame window open (keep refreshing the screen, etc.) but don't start a new life.

When the player is done, (done == True), the program also displays the final score.

I hope this helps make it clearer.

Regards,
Warren Sande
Carter will have a look at this and give you an answer after he gets home from school.

Regards,
Warren Sande
I kind of did that on purpose. I was hoping the reader would either do a web search to figure it out, or maybe look ahead in the table of contents and see "Print Formatting and Strings" in Chapter 21 and look there (e.g. page 275).

Trying to encourage a bit of "active learning" smilie

Glad you're enjoying the book

Regards,
Warren Sande
I just downloaded it and checked it with McAffee and Norton Anti-Virus. It came up clean.
This download has been in use for over a year without a problem.

It seems Kaspersky is somewhat prone to false positives for this. If you google "kaspersky false positive pdm.worm.p2p.generic". you'll find several instances of it.

I will have Manning IT look into this tomorrow as well.

Regards,
Warren Sande
Just to make sure we're on the same page, so to speak...

You would save the code below (which is Listing 15.1 in the book) as "my_module.py".

Then, in a separate program (e.g. "modular.py") you would put the line
import my_module

at the top (just like in Listing 15.2). Then my_module.py is used as a module, which means its code is treated like part of modular.py. These two files (my_module.py and modular.py) need to be saved in the same folder, otherwise modular.py won't be able to find my_module and won't be able to import it. Then you'll get the error about "no such module".

Hope this helps.

Regards,
Warren Sande
I would highly recommend you use our installer, here:

http://www.manning.com/sande/installers/hello_world_setup.exe

to install Python 2.5 (and all the modules, and SPE). There is no Windows installer for SPE for python 2.6. The Hello World installer works fine in Vista.

You can try copying the SPE source files, but you need to make sure you also have the correct version of wxPython for Python 2.6. SPE uses wx. It might not be very straightforward.

If you want to try it, you would download "SPE-0.8.4.c-wx2.6.1.0-no_setup.zip". Then unzip this, and you'll get a folder called "_spe". Copy that folder into c:python26libsite-packages.

So, you'll have c:python26libsite-packages\_spe All the SPE code will be in that folder.

You can find wxPython (for Python 2.6) here:
http://downloads.sourceforge.net/wxpython/wxPython2.8-win32-unicode-2.8.11.0-py26.exe

I can't guarantee that will work. We can really only support the installer that comes with the book.


Regards,
Warren Sande
That means either wx is not installed (correctly) or SPE can't find the installation of wx.

Again, I would recommend using the book's installer. It will take care of all of this for you.


Regards,
Warren Sande
SPE is itself a Python program. Just run spe.py.


Regards,
Warren Sande
THECOLORS is a dictionary of colors. We didn't cover dictionaries in the book at all, so it would have been difficult to provide a good explanation.

A Python dictionary is like a "hash table" or "associative array" in other languages. Basically, its an unordered list of key-value pairs. You can use the key to lookup a value.

Each entry in THECOLORS is a key-value pair where the key is the color name and the value is the hex color value (like "blue":0x0000FF)

'THECOLORS.keys' give you a list of the keys (the color names).

So, that code picks a random color name from list of key values.

Hope that's enough explanation for your purposes. You can look in the Pygame documentation to get more on THECOLORS, and you can also search online for more info about Python dictionalries.


Warren Sande
Glad you're enjoying the book.

I did try Geany some time ago, as an alternative to SPE. (Someone else on this forum recommended it.) I found that some of the examples in the book didn't run correctly. If I remember, the Pygame examples did not have smooth motion/animation. I didn't dig into it any further.


Warren Sande
Yes, but PythonCard is based on wxPython.

So, if you use a Python3-compatible version of wx, and then run the PythonCard stuff through a 2-to-3 converter, it might work. THere's probably lots of devils in the details, though. I haven't tried it yet.


Warren Sande
The book is written with Python 2.5, which was the most widely used version of Python at the time of publication. I'm pretty sure all the programs will work with 2.6, if you install versions of all the modules to go with it (Pygame, Pythoncard, Numeric or Numpy, wxPython, EasyGui).. I don't know about 2.7. It will definitely not work with Version 3.x.

(Personally, I think it's a shame they didn't make Python 3.x backwards compatible, or at the very least give us a "compatibility switch" so we could run Python 2.x programs without code changes.)

Our philosophy is that the book is about programming, not about one particular language or version of a language. It just happens to use Python (2.5) as a teaching tool. In 10 years, when Python is up to version 5.x (or whatever), Python 2.5, and our book, will still be perfectly valid for teaching programming. Just like many people still use BASIC or Pascal to learn programming.

My recommendation is to use the book's installer to install 2.5 and all the associated modules. Version compatibility is a bridge you can cross later.

By the way, can I ask where you purchased the book? It's always interesting to know through what channels readers are buying it.

Regards,
Warren Sande
I guess it would have been more correct to say, "Definitely will not work as-is with Python 3.x"

Of course you can convert the code to be Python 3.x compatible. I think that will be a little trickier with Pythoncard (and perhaps Pygame as well).

If you decide to try it, let me know how it goes.

Regards,
Warren Sande
sandefan,

Thanks for posting the results of your Python 3 testing. We may update the book at some point to Python 3, so this is interesting and useful.

If you get all the PythonCard programs to work as well, le us know!.


Regards,
Warren Sande
All the downloads for SPE can be found here:
http://developer.berlios.de/project/showfiles.php?group_id=4161

There is no windows .exe setup program for Python 2.5 there. Stani gave us one to use in our installer at our request.


Regards,
Warren Sande
You had the right idea. But instead of

if "Score: " == 1000

you need to use

if points == 1000

The variable named should not be in quotes. And the variable that keeps track of the score is called points, not score.

Then you would need a little code to display your end message, like this:

if points == 1000:
end_text = font.render("You Won!", 1, (0, 0, 0)) # create the end message
screen.blit(end_text, [200, 200]) #put the end message on the screen
pygame.display.flip() #update the display
pygame.time.delay(5000) #wait 5 seconds
sys.exit()

(This forum strips of the whitespace... Everything after the first line should be indented 4 spaces.)

This will display your end message for 5 seconds, then exit the program.

Hope this helps!


Warren Sande