[Tutor] Guess the number

Kewal Patel kp19.dev at gmail.com
Mon May 11 04:50:26 CEST 2015


i don't know if this is efficient but i think it works just fine....


import random

# input a number from user

input_number = input("Enter a number")

#function defination

def guess_number(start,stop):
    global input_number
    try:
        g_number = random.randrange(start,stop)
        if g_number == input_number :
            print "The Input Number is : ",g_number
        else:
            print "guessed number is :",g_number
            reply = raw_input("Enter higher or lower")
            if reply == "lower":
                guess_number(start,g_number)
            else:
                guess_number(g_number,stop)
    except ValueError,(ex):
        print "you have entered wrong answer."

guess_number(1,100)

On Thu, May 7, 2015 at 9:39 AM, Ikaia Leleiwi <ileleiwi at gmail.com> wrote:

> I am having trouble writing a program that guesses a number inputted by the
> user.  A number between 1-100 is inputted into the program and the computer
> produces a random integer that is the "guess" as to what the inputted
> number might be.  If the guess is lower then the inputted number then the
> user inputs the word 'higher' to indicate the computer needs to guess a
> higher number.  If the guess is higher than the inputted number then the
> user inputs the word 'lower' to indicate the computer needs to guess a
> lower number.
>
> My goal is to have this process repeat, continually narrowing down the
> range of possible numbers that the computer can guess, until it guesses the
> correct number.
>
> The code I have thus far is as follows:
>
> #Computer Guesses Number Game
> #The player chooses a number between 1 and 100
> #The computer guesses a number
> #The player inputs either higher or lower depending whether
> #the computer guesses higher than the chosen number or lower
> #When the computer guesses correctly it is congratulated
>
> import random
>
> number = int(input("Pick an integer between 1-100 "))
>
> guess = random.randint(1,100)
>
> while guess != number:
>
>     if guess < number:
>         print("The computer guesses: ",guess)
>         input("higher or lower ")
>         guess = random.randint(guess,100)
>
>     elif guess > number:
>         print("The computer guesses: ",guess)
>         input("higher or lower ")
>         guess = random.randint(1,guess)
>
>     else:
>         print("Congradulations Computer!! You guessed that the number was
> ",number)
>
> -----------------------------------
>
> I can't figure out how to narrow down the random integer range as the
> computer guesses closer and closer to the actual value of the number chosen
> in the beginning.
>
> Any help would be greatly appreciated
>
> Thanks,
> Kai
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list