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

Python guessing game

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

Problem

I'd like some feedback.

from random import randint

turns = 1
computer_guess = randint(1, 10)
while True:
    user_guess = raw_input("Guess Number: ")
    print 4-turns, "Turns left"
    if turns == 4:
        print "You lost!, LOOSER!"
        exit()
    else:
        if int(user_guess) == int(computer_guess):
            print "Congratulations, You won!\n"
            play_again = raw_input("Do You Want To Play Again? (Y / N): ")
            if play_again.upper() == 'Y':
                continue
            else:
                exit()
        else:
            turns += 1
            print "Try again - N was ", computer_guess

Solution

With some short code like that, I decided I'd run it to get a better feel for the program. Here's my first session:

Guess Number: 45
3 Turns left
Try again - N was  9
Guess Number: 9
2 Turns left
Congratulations, You won!

Do You Want To Play Again? (Y / N): yes


(end)

You didn't give any rules at the beginning of the file, so how was I to know the number had to be between one and ten? I know only because I looked at your code. I typed 45 and there was no indication that it was invalid. It told me it wasn't the right answer and said I had three turns left, but it also told me what the right answer was. That made my second guess easy. I was right, but it still printed 2 Turns left. I said yes when it asked if I wanted to play again, but it exited the program anyway. Another minor thing: you want to look professional and that includes more than just how the program works. It includes how your text is formatted. Your caPitaliZatioN maKes a diFfErence iN How yOur ProGram LooKs. You have a lot of unnecessarily capitalized letters. I would recommend looking up a good style guide on capitalization.

print 4-turns, ...


Why 4? Well, of course, it's because that's how many turns there are. It's an arbitrary number (called a magic number) that should instead be defined as a constant at the beginning of the file to be used in all cases instead of typing in magic numbers. You also shouldn't be printing how many turns are left unless the user did not win or lose. That means that it should come after the if and else blocks.

exit()


It is unnecessary to exit; you can just use break.

if int(user_guess) == int(computer_guess):


What if the user typed wheeeee!? It's a common thing to say, right? Well, anyway, the user might type something invalid and you wouldn't want to have an error if he did. You'd have a disgruntled user on your hands. You also don't need to convert computer_guess. It's already an integer.

if play_again.upper() == 'Y':
    continue
else:
    exit()


You shouldn't just continue. You also need to reset turns.

You shouldn't use a bare else. What if the user typed yes as I did? It still exits. You should use a loop there that validates the user's input. If you want to be really fancy, define a function that excepts a bunch of different inputs as positive and a bunch as negative. I once wrote a function that even counted uh huh as a valid way of saying yes.

print "Try again - N was ", computer_guess


You shouldn't tell the user what the answer is until the game is over.

The new code:

from random import randint

TOTAL_TURNS = 4

turns = 1
computer_guess = randint(1, 10)
while True:
    try:
        user_guess = int(raw_input("Guess number between 1 and 10: "))
    except ValueError:
        print "Please type an integer."
        continue
    if not 1 <= user_guess <= 10:
        print "Please type a number between 1 and 10."
        continue
    if turns == TOTAL_TURNS:
        print "You lost! Loser!"
        break
    else:
        if user_guess == computer_guess:
            print "Congratulations, You won!\n"
            while True:
                play_again = raw_input("Do you Want To Play Again? (Y / N): ").upper()
                if play_again in "YN": break
                print "Please type Y / N"
            if play_again == 'Y':
                turns = 1
                computer_guess = randint(1, 10)
                continue
            else:
                break
        else:
            turns += 1
            print "Try again"
    print TOTAL_TURNS - turns + 1, "Turns left"

Code Snippets

Guess Number: 45
3 Turns left
Try again - N was  9
Guess Number: 9
2 Turns left
Congratulations, You won!

Do You Want To Play Again? (Y / N): yes
print 4-turns, ...
if int(user_guess) == int(computer_guess):
if play_again.upper() == 'Y':
    continue
else:
    exit()
print "Try again - N was ", computer_guess

Context

StackExchange Code Review Q#125340, answer score: 2

Revisions (0)

No revisions yet.