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

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Noob needs help with game issues!


    Hey everyone! I'm new to this forum and new to programming. I picked up Python yesterday and working at it determinedly. I have made a few calculators and I decided to make a game using what I knew. For some reason the game works about half the time, and about the other half of the time it ends either during the 2nd or 3rd turn. (Could be higher, but all I've gotten playing it so far) The game is a guess a number between... game. Here is what I have:
    Code:
    print("Guess the number game!")
    
    print("What is your name, player?")
    
    playerName = input()
    
    print("Try to guess my number.  It's between 1 and 100!")
    
    import random
    
    Number = random.randint(1, 100)
    
    print('What is your first guess?')
    
    GuessCounter = 0
    Guess1 = int(input())
    while (Guess1) > (Number):
        print("It's lower!")
        Guess1 = int(input ())
        (int(GuessCounter + 1))
    while Guess1 < Number:
        print("It's higher!")
        Guess1 = int(input ())
        (int(GuessCounter + 1))
    if Guess1 == Number:
        print('Wahooooo!  You got it!')
    
    print('The number was', Number)
    
    print(GuessCounter)
    
    print('Thanks for playing,', playerName, '!')
    Please help! Also, being a noob, I'm not up to par with coding standards. If you notice anything I could do that could make my work better or more consistent, please let me know!
    *Edit: Python 3.3.2, Windows.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    We don't know the rules you intend your game to enforce. Thus we do not know the program problem. Still, you probably meant
    Code:
    # python 3
    
    import math, random
    
    
    print("Guess the number game!")
    print("What is your name, player?")
    playerName = input()
    print("Try to guess my number.  It's between 1 and 100!")
    
    Number = random.randint(1, 100)
    prompt = 'What is your first guess?'
    GuessCounter = 0
    
    while GuessCounter < math.log(100, 2):
    
        while True: # trap bad input
            try:
                Guess1 = int(input(prompt+' '))
            except:
                prompt = '??? integer please:'
            else:
                break
    
        GuessCounter += 1
        if Number < Guess1:
            prompt = "It's lower!"
        elif Guess1 < Number:
            prompt = "It's higher!"
        else:    
            print('Wahooooo!  You got it!')
            break
    
    print('The number was', Number)
    print(GuessCounter)
    print('Thanks for playing,', playerName, '!')

    Comments on this post

    • TheRealJR agrees : Very helpful!
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Thank you! This helped! Maybe I'm a bit over my head. You seem like you really know your stuff! I'm just starting out so I've been using a myriad of different internet resources/this android app but I'd really like to find something concrete to start learning from. Any suggestions?

IMN logo majestic logo threadwatch logo seochat tools logo