HiLo Game - Python

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
So i'm taking an edX course for programming and computer science just for the fun of it based from MIT. Haha, it's kind of easy right now... First lecture, was to install python, and since I had nothing else to do but watch a bunch of boring videos, I came up with a HiLo game in python and learned how to import.

Python is too much of an easy language to understand, and I think that's why they chose it.

Code:
import random

# The number the user is trying to guess
x = int(random.uniform(1, 100))

# The user's guess for the current try
g = 0

# The number of tries taken to guess the number (counter)
numG = 0

while g != x:
    # Take in user's guess and increment counter
    g = int(raw_input('Take a guess (1-100): '))
    numG += 1

    # Check user's guess to tell them higher or lower
    if g > x:
        print('Your guess was too high! Guess lower.')
    elif g < x:
        print('Your guess was too low! Guess higher.')

# Game finished, user found the guess number
print('You finally guessed the number in ' + str(numG) + ' tries.')
 
You don't normally post things that simple. No error checking though. :lol:

I like python, it's simple but you can do a lot with it. For app programming I'm trying to get into C++ instead, but I've had fun making python games. The only problem was I wasn't really sure where to go with python after I'd learnt the basic syntax.
 
lol, oh yeah, I guess I should check the input. Simple things like this bore me though, I actually (sadly), just created this so I could play some game while i was waiting for my lecture content to download :lol:

I like taking on challenges though. :)

edit: Alright, how does this look?
Code:
import random

# The number the user is trying to guess
x = int(random.uniform(1, 100))

# The user's guess for the current try
g = 0

# The number of tries taken to guess the number (counter)
numG = 0

while g != x:
    # Check user input
    valid = False
    while(not valid):
        try:
            g = int(raw_input('Take a guess (1-100): '))
            valid = True
        except:
            print('Please enter a valid integer.')
    # Increment guess counter
    numG += 1

    # Check user's guess to tell them higher or lower
    if g > x:
        print('Your guess was too high! Guess lower.')
    elif g < x:
        print('Your guess was too low! Guess higher.')

# Game finished, user found the guess number
print('You finally guessed the number in ' + str(numG) + ' tries.')
raw_input()

I was running it outside of the interpreter, so the window would close, otherwise I wouldn't need that last raw_input() line.

edit: Just found out that the random.py module has a randint() function that I could have used instead, I think i'm okay with where this is at though, It's just going to be another code snippet of mine on my file storage to collect dust for a while now that It doesn't entertain me anymore :grin1:
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top