#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Help with python guess the number


    Hi, I'm am very very new to programming and python is my first coding language, Ive read tutorials online and they showed me how to make a guess the number game, so i did, and it worked fine, but i tried to make my own with different difficulty settings, and on easy it say 1-10 and when you guess 7 its too high, but 6 is too low, Ive set an if number == guess, and Ive changed the input to an integer its just not matching up :/


    Code:
    import random
    guesses = 0
    print ('welcome to the guess the number game, whats your name?')
    name = input()
    print()
    print('nice to meet you ' + name + ', please select your difficulty level:')
    print()
    print('easy')
    print('normal')
    print('hard')
    difficulty = input()
    while difficulty == 'easy':
        while guesses < 7:
            print ('Im thinking of a number between 1 and 20')
            number = random.randint(2, 19)
            print('take a guess...')
            guess = int(input())
            guesses = guesses + 1
            if guess < number:
                print('that number is too low')
            if guess > number:
                print('that number is too high')
            if guess == number:
                difficulty = 4 #i tried to set it as none first but i got an error,
                break          #4 is just a random thing i threw in there to cut off
    while difficulty == 'normal':#easy, normal, and hard
        while guesses < 6:
            print ('Im thinking of a number between 1 and 25')
            number = random.randint(2, 24)
            print('take a guess...')
            guess = int(input())
            guesses = guesses + 1
            if guess < number:
                print('that number is too low')
            if guess > number:
                print('that number is too high')
            if guess == number:
                difficulty = 4
                break
    while difficulty == 'hard':
        while guesses < 5:
            print ('Im thinking of a number between 1 and 30')
            number = random.randint(2, 29)
            print('take a guess...')
            guess = int(input())
            guesses = guesses + 1
            if guess < number:
                print('that number is too low')
            if guess > number:
                print('that number is too high')
            if guess == number:
                difficulty = 4
                break
    if guess == number:
        guesses = str(guessesTaken)
        print('Good job, ' + name + '! You guessed my number in ' + guesses + ' guesses!')
    if guess != number:
        number = str(number)
        print('Nope. The number I was thinking of was ' + number)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    (Gentle readers please take notice: this is a python3 code.)

    I played your game once at the easy level. It worked correctly until I got to the undefined `guessesTaken' variable which should be `guesses'. Again, please, what is the trouble?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    oops I didn't mean to push the submit button yet.

    You have the comment
    #i tried to set it as none first but i got an error,

    None is spelled with a capital N as in None and that could be the trouble.


    And, oops, your game only worked by luck. The problem is is that you keep changing the target number.
    number=random.randint(2,maximum)
    needs to be outside the innermost while loop.
    See rewrite. You have a lot of duplicate code. Soon you'll learn to write functions. You'll also learn about the else and elif clauses on an if statement.
    Code:
    import random
    
    def game(maximum,limit):
        guesses = limit
        number = random.randint(2, maximum-1) # CHOOSE THE NUMBER HERE, JUST ONE TIME
        message = "I'm thinking of a number between 1 and {}".format(maximum)
        while guesses:
            print(message)
            print('take a guess...')
            guess = int(input())
            guesses -= 1
            if guess < number:
                print('that number is too low')
            elif guess > number:
                print('that number is too high')
            else: # they must be equal
                return (limit-guesses,number,)
        return (0,number,)
    
    print ('welcome to the guess the number game, whats your name?')
    name = input()
    print()
    print('nice to meet you ' + name + ', please select your difficulty level:')
    print()
    print('easy')
    print('normal')
    print('hard')
    difficulty = input()
    difficulty_arguments = dict(easy=(20,7),normal=(25,6),hard=(30,5))
    try:
        arguments = difficulty_arguments[difficulty]
    except KeyError:
        arguments = difficulty_arguments['normal']
    
    (guesses_taken,number,) = game(*arguments)
    
    if guesses_taken:
        print('Good job, {}! You guessed my number in {} guesses!'.format(name,guesses_taken))
    else:
        print('Nope. The number I was thinking of was {}'.format(number))

    Comments on this post

    • bloodycage agrees
    Last edited by b49P23TIvg; July 22nd, 2012 at 08:12 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Thanks


    i understand now, thank you so much and sorry for the trouble i was just stumped, Lol, i understand the how the new code works in general, theres just a couple things i didn't get, but I'm sure ill get to it very soon, thanks again

IMN logo majestic logo threadwatch logo seochat tools logo