Newbie: Explain My Problem

Brian van den Broek bvande at po-box.mcgill.ca
Wed Jun 29 03:59:06 EDT 2005


ChuckDubya at gmail.com said unto the world upon 29/06/2005 03:11:
> Code:
> 
> #The Guess My Number Game
> 
> import random
> num = ""
> guess = ""
> counter = 7
> num = random.randrange(1, 100)
> print "I'm thinking of a whole number from 1 to 100."
> print "You have ", counter, " chances left to guess the number."
> print
> guess = int(raw_input("Your guess is: "))
> while counter != 0:
>     if guess == num:
>         print "You guessed the number, ", num, " in ", counter-6, "
> guesses!"
>     elif guess > num:
>         counter = counter - 1
>         print
>         print "The number is less than your guess."
>         print "You have ", counter, " chances left to guess the
> number."
>         guess = int(raw_input("Your guess is: "))
>     else:
>         counter = counter - 1
>         print
>         print "The number is greater than your guess."
>         print "You have", counter, " chances left to guess the number."
>         guess = (raw_input("Your guess is "))
> if counter == 0:
>     print "You idiot, my number was", num,"!"
>     print "YOU LOSE!"
>     raw_input("Hit the enter key to exit.")
> 
> 
> Two things wrong happen:
> - Dialogue switches from saying "number is greater" to "number is
> less", regardless of guess
> - Lets user guess when user has no more guesses left in "counter"
> variable.
> 
> Please explain to me what's wrong with my program.
> 

Well, you have some logic problems, and they are harder to see because 
of some structural problems.

Notice that in your elif and else branches you repeat logic? Or 
rather, almost repeat logic :-) (You left of the conversion to int on 
one of the calls to raw_input.)

You also had the problem that if the user was right, they'd be told so 
quite a few times ;-)

And, you weren't keeping track of the guesses properly.

Compare yours with the code below. I've moved things around, 
eliminated the duplication (and near duplication), removed the 
pointless initial assignments to num and guess, closed the infinite 
loop, and built the strings differently.

import random

counter = 7
num = random.randrange(1, 100)

print "I'm thinking of a whole number from 1 to 100."

while counter != 0:
     print "You have %s chances left to guess the number." %counter
     guess = int(raw_input("Your guess is: "))
     counter = counter - 1

     if guess == num:
         print "You guessed the number, %s in %s guesses!" %(num, 
7-counter)
         break     # else it will print success msg forever

     elif guess > num:
         print "\nThe number is less than your guess."

     else:
         print "\nThe number is greater than your guess."

if counter == 0:
     print "You idiot, my number was %s!" %num
     print "YOU LOSE!"
     raw_input("Hit the enter key to exit.")


This still assumes a co-operative user. (Try entering "one" and see 
what happens.) "1 chances" looks goofy, too. You might want to think 
about fixing that.

Best,

Brian vdB




More information about the Python-list mailing list