HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Number guessing game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamenumberguessing

Problem

i'm a beginner and this is the second program that i have written by myself from scratch, i wrote a basic snake game with PYgame but i didn't do it by myself, so anyway, what do you guys think of this? i want honest opinions, no sugar-coating ! does it have any problems? is there any improvements that can be done?

import random
print "Guess my number!"
print " What's your name?"
user_name = raw_input()
print "Hello %s, i'm thinking of a number between 1 and 20." %(user_name)
def gameloop():
    random_number = random.randrange(1,21)
    print "Take a guess."
    vectory_message = "Well done %s, You beat me!" %(user_name)
    defeat_message = "Sorry %s, You lost!" %(user_name)
    attempts = 0
    maximum_attempts = 7
    user_number = int(raw_input())
    attempts += 1
    print "You have %s  lives left." %(maximum_attempts - attempts)
    while True:
        if user_number == random_number:
            print vectory_message
            attempts = 0
            second_chance()

        elif user_number > random_number:
            user_number  = int(raw_input("A bit lower ..."))
            attempts += 1
            if attempts == maximum_attempts:
                print defeat_message
                second_chance()
            print "You have %s  lives left." %(maximum_attempts - attempts)
            continue

        elif user_number < random_number:
            user_number = int(raw_input("Guess higher ..."))
            attempts += 1
            if attempts == maximum_attempts:
                print defeat_message
                second_chance()
            print "You have %s  lives left." %(maximum_attempts - attempts)
            continue

def second_chance():
    while True:
        choice = raw_input("wanna give it another shot?(y/n)")
        if choice == "y":
            gameloop()
        elif choice == "n":
            quit()
        else:
            print "That wasn't a (y) or (n)!, Try again."
            continue    
gameloop()

Solution

Your code is pretty good, but some of your logic isn't.

The game loop should be structured differently.
Also I'm ignoring second_chance.

There are two main things that should happen after I pick a number:

  • I'm correct, you say I win and the game ends.



  • I'm wrong, you tell me if I'm higher or lower.



This in psudocode could be:

while True:
    user-input = get-user-input()
    if user-input == random-number:
        stop game
    else:
        display if user is higher or lower.


This should highlight that it's pretty much Python.
And we could do with another function.

First, stop game should exit out of the game, and tell us if we won or not.
This can simply be return True.

The logic to display if the user is higher or lower then the guess is an if and an else.
If the guess is larger then you want to display "A bit lower ..." otherwise (the else) "Guess higher ...".
To do this simpler I will use a turnery operator.

And so it'd be:

guess_text = "Take a guess."
random_number = random.randrange(1,21)
while True:
    user_number = int(raw_input(guess_text))
    if user_number == random_number:
        return True
    else:
        guess_text = "A bit lower..." if user_number > random_number else "Guess higher..."


Ok so we now have a solid loop, but it's not much of a game.
And the rest of your elif's are pretty good here.
I'd change the attempts = 0 to return false and remove the continue.

This should get you:

guess_text = "Take a guess."
random_number = random.randrange(1,21)
attempts = 0
maximum_attempts = 7
while True:
    user_number = int(raw_input(guess_text))
    if user_number == random_number:
        print "Well done %s, You beat me!" %(user_name)
        return True
    else:
        guess_text = "A bit lower..." if user_number > random_number else "Guess higher..."

        attempts += 1
        if attempts == maximum_attempts:
            print "Sorry %s, You lost!" %(user_name)
            return False
        print "You have %s  lives left." %(maximum_attempts - attempts)


Now you should know that return is only allowed in a function.
This means you need to make a function that gets passed the user_name.
So:

def play_game(user_name):
    guess_text = "Take a guess."
    attempts = 0
    ...


Finally you want to be able to play multiple games!
I'll use second_chance for the basis for this loop.

  • You should change quit() to break.



  • Remove the continue.



  • Put all the prints and definition for user_name at the top (not in the loop).



  • Rename it as main.



This should get you:

def main():
    print "Guess my number!"
    print " What's your name?"
    user_name = raw_input()
    print "Hello %s, i'm thinking of a number between 1 and 20." %(user_name)
    while True:
        choice = raw_input("wanna give it another shot?(y/n)")
        if choice == "y":
            play_game(user_name)
        elif choice == "n":
            break
        else:
            print "That wasn't a (y) or (n)!, Try again."


Finally I'd recommend using str.format over %.

So as you should guess, your code was pretty good!
Apart from the duplicate questions.

Code Snippets

while True:
    user-input = get-user-input()
    if user-input == random-number:
        stop game
    else:
        display if user is higher or lower.
guess_text = "Take a guess."
random_number = random.randrange(1,21)
while True:
    user_number = int(raw_input(guess_text))
    if user_number == random_number:
        return True
    else:
        guess_text = "A bit lower..." if user_number > random_number else "Guess higher..."
guess_text = "Take a guess."
random_number = random.randrange(1,21)
attempts = 0
maximum_attempts = 7
while True:
    user_number = int(raw_input(guess_text))
    if user_number == random_number:
        print "Well done %s, You beat me!" %(user_name)
        return True
    else:
        guess_text = "A bit lower..." if user_number > random_number else "Guess higher..."

        attempts += 1
        if attempts == maximum_attempts:
            print "Sorry %s, You lost!" %(user_name)
            return False
        print "You have %s  lives left." %(maximum_attempts - attempts)
def play_game(user_name):
    guess_text = "Take a guess."
    attempts = 0
    ...
def main():
    print "Guess my number!"
    print " What's your name?"
    user_name = raw_input()
    print "Hello %s, i'm thinking of a number between 1 and 20." %(user_name)
    while True:
        choice = raw_input("wanna give it another shot?(y/n)")
        if choice == "y":
            play_game(user_name)
        elif choice == "n":
            break
        else:
            print "That wasn't a (y) or (n)!, Try again."

Context

StackExchange Code Review Q#126105, answer score: 3

Revisions (0)

No revisions yet.