[Tutor] Help with Python Program

Steven D'Aprano steve at pearwood.info
Sat Feb 25 04:59:45 CET 2012


Carolina Dianne LaCourse wrote:

[...]
> I understand that I need to ask for raw input from the user and that I
> need to be able to use the if elif else to tell the user whether their
> number matches or id too high or to low but am just not sure what to
> do first. Any advice would be greatly appreciated! I have tried some
> online tutorials to get the basics but still am having a really hard
> time. I did work with scratch a bit earlier this semester and got
> that, but am really struggling with python.

Start by writing down in plain English the steps of how you would play the 
guessing game. This is a called an algorithm, which is something very similar 
to a recipe or a set of instructions. You might have something like this:

(1) Think of a number between 1 and 100, and remember it.
(2) Repeat the following steps until the game is over:
(3) - Ask the person playing for a number between 1 and 100.
(4) - If the number is too low, tell them it is too low.
(5) - If the number is too high, tell them it is too high.
(6) - If the number is equal to the number you thought of, the game is over.

All that makes up *one* game. Then you need instructions to play multiple games:

(a) Play one game, as above.
(b) Repeat the following steps until done:
(c) - Ask the player if they want to play again.
(d) - If they say Yes, play one game, as above.
(e) - Otherwise, we are done.
(f) Finally, print how many games were played, how many guesses were needed, 
and the average number of guesses per game.


Now, you need to change the English instructions to instructions the computer 
can follow, using Python. For example, Step (1) above picks a random number 
and remembers it as the target of the game:

import random
target = random.randint(1, 100)

Step (2) is a bit harder -- it's a loop. You should have learned about while 
loops and for loops. I expect a while loop is better for this, because you 
can't tell ahead of time how many times you need to go round and round the loop.


while guess != target:
     Step (3) ask the user for a number, and call it "guess"
     if guess < target:
         print "too low"
     elif guess > target:
         print "too high"
     # otherwise guess == target so the game will be over


Notice that this isn't exactly Python code. The most obvious problem is the 
line "Step (3)..." which is plain English. You need to replace that with code 
to actually ask the user for a number. (Hint: you will need the raw_input 
function.)

Another problem is that the *first* time you enter the loop, the name "guess" 
isn't defined. You need to give it a value to start with, before the loop. Any 
value will do, so long as it isn't target. I suggest 0.

Does this help you get started? Go ahead and write some code, and see where it 
takes you. Piece by piece, step by step, you should work towards replacing 
each bit of English instruction with some Python code to do that.

You should aim to write code to play *one* game first. Get that right, first, 
then adapt it to play multiple games.

Write some code, see how it works (or where is fails to work), and anything 
that is unclear, come back and ask.



-- 
Steven



More information about the Tutor mailing list