[Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4

John Feleppa john.feleppa at gmail.com
Fri Aug 9 09:51:20 CEST 2013


Thanks for getting back to me...

I am using Python 3.3.2.

The challenge is as follows:

# Chapter 3 Challenge 4
#
# Write the psudocode for a program where the player and the computer
# trade places in the number guessing game.  That is, the player picks a
# random number between 1 and 100 that the computer has to guess.
# Before you start, think about how you guess.  If all goes well, try
# coding the game.

It builds on the previous challenge, where the user guesses the number - I
coded that OK, as below.

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop

while guess != the_number and tries < 10:
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher...")

    guess = int(input("Take a guess: "))
    tries += 1

if guess == the_number:
    print("You guessed it!  The number was", the_number)
    print("And it only took you", tries, "tries!\n")
else:
    print("Huh - you took ", tries, "tries and still never got it.")
    print("It was ", the_number, " - but why tell you, you'll forget it,
won't you.")

input("\n\nPress the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130809/b9c24477/attachment.html>


More information about the Tutor mailing list