Very basic question. How do I start again?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 21 22:58:40 EDT 2014


Seymore4Head wrote:

> I want to give the computer 100 tries to guess a random number between
> 1 and 100 picked by the computer.
> 
> For the moment I am always using 37 as the random pick.  I want to
> change the pick to pick=random.randrange(1,100).  The program works as
> expected until the computer gets a correct guess.  I don't know what I
> should be doing to restart the program when pick=guess.
> 
> It is supposed to let the computer pick a number between 1 and 100 and
> then let the computer guess the answer.  If the computer picks a low
> number the next guess is supposed to be limited to higher numbers than
> the guess.  If the computer picks a high number, the next guess is
> supposed to be limited to lower numbers than the first guess.
> 
> The program fails when guess=pick
> 
> import random
> count = 1      #Start the counter at 1
> low=1           # the low range of 1 to 10
> high=100      #The high range of 1 to 100
> pick = 37      # Will change to pick=random.randrange(1,100)
> guess = 0     #Guess is the computer's guess at pick
> print ("Time to play a guessing game.")
> print ("")
> 
> 
> while count < 100:
>     guess = random.randrange(low,high)
>     print (pick, guess)
>     if guess == pick:
>         print ("correct")
> 
> #"What I need is something here that says start over"

Start over as in "go back to the beginning".

There is no specific way to jump backwards to a previous line of code in
Python. Python has two ways to loop, `while` and `for`. In this case
`while` is the answer.

The idea is to think about the high-level structure of the game.

# Not python code, this is pseudo-code
while you want to play again:
    play a game
    ask "Do you want to play again?"


Now you can fill in some of the details, using a stub for things which will
be filled in later.

again = True  # Start off wanting to play at least once.
while again:
    print("pretend that we just played a game")  # a stub
    # Use input in Python 3, raw_input in Python 2.
    again = input("Would you like to play again? [y/n] ") == "y"


That will (pretend to) play a game at least once, then give you the choice
to continue or not. If you would rather play a fixed number of games:

for counter in range(10):  # Play 10 games.
    print("pretend that we just played a game")


Now let's stop pretending:


import random
again = True  # Start off wanting to play at least once.
while again:
    # Here we actually play the game.
    count = 1      # Start the counter at 1
    low = 1        # The low range of 1 to 100
    high = 100     # The high range of 1 to 100
    pick = random.randrange(1,100)
    guess = 0      # Guess is the computer's guess at pick
    print("Time to play a guessing game.")
    print()
    while count < 100:
        guess = random.randrange(low,high)
        print(pick, guess)
        if guess == pick:
            print("Correct!")
            # Break out of this loop, which returns us to
            # the outer loop.
            break

        elif guess < pick:
            # And so on...

    # Use input in Python 3, raw_input in Python 2.
    again = input("Would you like to play again? [y/n] ") == "y"



Notice that indentation is important: the size of the indent tells Python
whether you are inside the inner loop, the outer loop, or the top level.
But hopefully you already know that.

As this starts getting bigger and more unwieldy, it's time to learn about
defining your own custom functions. But you'll get to that.


-- 
Steven




More information about the Python-list mailing list