[Tutor] (no subject)

Kevin Johnson kevin.johnson2711 at gmail.com
Thu May 8 12:00:11 CEST 2014


Hi,

Total beginner to python and am working my way through Michael Dawsons
'Absolute beginner' book. Just got stuck on the last bit of the challenges
from chapter 3. Essentially need to create a game where the user picks a
number between 1 and 100 and the computer has to guess, program should
indicate to the computer if the guess need to be higher or lower, it should
also count the number of attempts and call a halt to the game if a set
number of attempts is reached.

The highlighted bit is where I think I'm going wrong but I just can't think
how to make the computer remember the previously closest highest and lowest
guesses, not sure if you have all read Micheal Dawsons book but I've not
covered a whole lot by chapter 3 and so any hints/solutions need to be
pretty basic as I don't want to get ahead of myself.

Thanks,
Kevin

#numbers game again but...
#user picks number between 1 and 100
#and computer guesses

import random

user_number = int(input("Pick a number between 1 and 100: "))
computer_guess = random.randint(1, 100)
guesses = 1

while computer_guess != user_number:
    print("The computers guess is", computer_guess)
    if computer_guess > user_number:
        print("Lower...")
    else:
        print("Higher...")
    if guesses == 10:
        print("You lose computer!")
        break
    if computer_guess > user_number:
        computer_guess = random.randrange(1, (computer_guess-1))
    else:
        computer_guess = random.randint((computer_guess + 1), 100)
    guesses += 1
    if computer_guess == user_number:
        print("Well done you guessed the number was", user_number)
        print("It took you", guesses, "tries")

input("Press the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140508/5453366a/attachment.html>


More information about the Tutor mailing list