Number Game

The number game goes like this.  I’m thinking of a number between one and one hundred.  Take a guess…

57

No, my number is lower than that.  Take another guess…

And so on.

Except the computer is the one who thinks of the number, and the user is the guesser.  This kind of program, where the same thing happens over and over, uses a loop.  When you use a loop, you need to decide what goes BEFORE the loop, what goes INSIDE the loop, and what goes AFTER the loop.    The loop code looks like this.

[CODE THAT GOES BEFORE THE LOOP]

while True:
[CODE THAT IS INSIDE THE LOOP]
[SOMEWHERE INSIDE THE LOOP, YOU HAVE TO SAY break, OR THE LOOP WILL GO FOREVER.]
end
[CODE THAT GOES AFTER THE LOOP]

One thing that goes before the loop is choosing a random number and saving it in a variable.

from random import randint
number = randint(1,100)

One thing that you do inside the loop is let the user guess the number.  This looks like this:

guess = get_int("What is your guess? ")

Once you have the two numbers, called number and guess, you can write an if…elif…else statement to figure out what happens.

Make sure all this is inside the loop.

Give this a try.  Once you have the number game working, see what you can do to make it better.