Tqst Drove

To run the program, you need to make sure that the edit window for the game . py program code is selected. Each time you run (or rerun) the program, you need to click on the IDLE edit window and choose the Run Module option from the Run menu. The word module is a name that IDLE uses to refer to your program code.

Here's what happens when you run the code:

Me* you ru* your Code within ID LB,

Shell not wi-thin the edit window IPLb automatically makes the shell the ¿gently selected window the insbnt your program runs.

Python Shell

File frtit r.h.fll r^hug nptmn«; Window1;

i'ytfcon J.U.I (riUlifcUïïfc [fiCr 4. .1.1) E hi I limn? Type "copyright", "credith"

Wclcornc I

Wcicomc!

or "HcenBe[)~ tor [».ore lntonnatlon. —„„---RnSTftST------------------

A toupie of incorrect guesses..

A toupie of incorrect guesses..

Congratulations! The program works.

Each time you run the code, it displays a "Welcome!" message, accepts input from the keyboard, and then tells us whether or not we guessed the right answer. That means the program is accepting input, it's processing the data, and then it generates output.

thereiqre nQ

Dumb Questions

Q/ I've never heard of Python. Is it popular?

A" Python is used in lots of cool places. Google, Industrial Light & Magic, YouTube, and NASA (to name a few) all use Python. We think they know what they're doing.

Q,* So, when I'm done with this book I'll throw Python away and use something else, like C# or Java?

A" Only if you want to. Python might be the only programming language you'll ever need. But, yes, if you want to learn another programming language, you can take everything you learn about programming in this book and apply it to any other language with the minimum of effort.

Q^* But a buddy of mine told me I should learn Java or C#. Why are you not using either of these programming languages in this book?

A." Both Java and C# are great programming technologies, but they can be difficult to learn, especially when you are just starting out. This is not the case with Python. And, anyway, this is a book that's designed to teach you how to program, and using Python as your first programming language will help us to do just that.

Q^* There seems to be many different versions of Python. Which should I use?

A" There are two main releases of Python: 2 and 3. This book is based on release 3 of the language. Python 3 is the future of the language; any new features are guaranteed to be added to release 3 of the language, not release 2. Of course, like all releases, Python 3 remains a free download, which makes it a no-brainer when decidiing if you can afford to use it.

Q^* Will Python run on my phone, just like Java?

A." That really depends on your phone. Python is designed to run on lots of different technologies and operating systems. Running your own code on your own phone is a very specific requirement, and Java has that pretty well covered at the moment. As a programming technology, Java was initially designed to run on very small devices, so it is no big surprise that it is a strong and popular choice when it comes to telephony.

Q* Why is the Python IDE called IDLE?

A." It's partly because it sounds like IDE, but we suspect that it has more to do with Eric Idle, one of the founding members of the Monty Python's Flying Circus comedy group.

Q^* Come again?!? Monty Python's Flying what?

A" Circus. Yes, we know: sounds silly, doesn't it? And, believe us, it is. It's funny, too. The creator of Python, Guido van Rossum, is a big Monthy Python fan and reportedly watched the show's reruns while he designed Python. You'll find lots of references to Monty Python folklore in the Python community. Dead parrots are a particular favorite.

A." It tells Python to interpret the user's input as a number rather than a letter. Within programming languages, the number 5 is different than the letter '5'.

A" The computer would have treated the input entered by the user of the program as a letter. If you ask the computer if a letter is equal to a number, it gets confused and tells you it isn't.

A" Because if the computer thinks that two pieces of information are of different "types," it assumes that there's no way they can be equal.

So what if I had not typed a number when I was asked for a guess? What if I'd just entered my name or something?

A" The code would have crashed with an error. In fact, Python will complain that the program crashed with a "ValueError" (more on these error messages later in the book).

I don't get it. How am I supposed to guess the winning number? All the program tells me is that my guess is right or wrong. Come on, give me some help here!

The program needs to do more.

At the moment, the guessing game tells the user whether his guess is right or wrong, but nothing more than that. It might be more helpful if the program displayed more informative messages, such as whether the guess is higher or lower than the correct answer. That would help the user to hone in on the right answer the next time the program is run.

We can do this by changing the code. But in what way?

We need "tW ^o^a* ■bo display messages -that ave more m-foviw atWe-

You need to decide what messages should be displayed to the user. Below is a table showing some typical values the user might enter. What do you think the message should say?

Think about the original code. You will need to use more than just print () commands to provide more informative feedback. What else will you need?

You needed to decide what messages should be displayed to the user. Below is a table showing some typical values the user might enter. What did you think the message should say?

Was this article helpful?

0 0

Post a comment