Newbie: Explain My Problem

C

ChuckDubya

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.
 
B

Brian van den Broek

(e-mail address removed) 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
 
R

rjreeves

Hi Chuck

1. Missing an int(.. on one of the guess=

print "You have", counter, " chances left to guess the number."

guess = int(raw_input("Your guess is "))

Thus ensuring guess is consistent with it content type

2. Need a break when the guess=num
if guess == num:
print "You guessed the number, ", num, " in ", counter-6, "
guesses!"
break

To stop from looping in the while when counter is NOT zero but the
number has been found.
 
R

rjreeves

Hi Chuck

1. Missing an int(.. on one of the guess=

print "You have", counter, " chances left to guess the number."

guess = int(raw_input("Your guess is "))

Thus ensuring guess is consistent with it content type

2. Need a break when the guess=num
if guess == num:
print "You guessed the number, ", num, " in ", counter-6, "
guesses!"
break

To stop from looping in the while when counter is NOT zero but the
number has been found.
 
R

Roel Schroeven

Code:
[snip]

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 "))

The above line is incorrect: it should be
guess = int(raw_input("Your guess is "))
 
J

John Machin

while counter != 0:
if guess == num:
[snip]

Others have told you already what was wrong with your program. Here's a
clue on how you could possibly help yourself:

1. Each time around your loop, print the values of the interesting
objects, in this case counter and guess.
E.g. before the "if" statement above, put something like this:
print "guess = %r, counter = %r" % (guess, counter)
That would have shown "guess" containing a string e.g.
guess = '42', counter = 4
instead of an integer e.g.
guess = 42, counter = 4

2. Examine the logic carefully. The answer to your second problem was
just staring you in the face -- you hadn't pulled the ripcord after the
counter became zero.

Cheers,
John
 
R

Robert Kern

John said:
while counter != 0:
if guess == num:

[snip]

Others have told you already what was wrong with your program. Here's a
clue on how you could possibly help yourself:

1. Each time around your loop, print the values of the interesting
objects, in this case counter and guess.
E.g. before the "if" statement above, put something like this:
print "guess = %r, counter = %r" % (guess, counter)
That would have shown "guess" containing a string e.g.
guess = '42', counter = 4
instead of an integer e.g.
guess = 42, counter = 4

2. Examine the logic carefully. The answer to your second problem was
just staring you in the face -- you hadn't pulled the ripcord after the
counter became zero.

3. Sign up for the Python-tutor list. (Okay, that's not quite "helping
yourself," but it's still good advice, I think).

http://mail.python.org/mailman/listinfo/tutor

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,301
Members
47,944
Latest member
LillianPra

Latest Threads

Top