Newbie help

C

Chad Everett

Hey guys,

Hope you can help me again with another problem. I am trying to learn
Python on my own and need some help with the following.

I am writing a program that lets has the pc pick a number and the user
has five guess to get the number.
1. BUG: If the number is say 35 and I guess 41 the program tells me
that I guessed the correct number and tells me I guessed 31.

2.When I do get the correct number I can not get the program to stop
asking me for the number.


Your help is greatly appreciated.

Chad

# Five Tries to Guess My Number
#
# The computer picks a random number between 1 and 100
# The player gets Five tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
#
# Chad Everett 2/10/2005

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "You Only Have Five Guesses.\n"

# set the initial values
number = random.randrange(100) + 1
guess = int(raw_input("Go Ahead and Take a guess: "))
tries = 1


# guessing loop


while guess != number:

if (guess > number):
print "Guess Lower..."
else:
print "Guess Higher..."

guess = int(raw_input("Take Another guess: "))
tries += 1

print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"

if tries == 5:
print "Sorry You Lose!!!!"
print "The Number was ", number

raw_input("\n\nPress the enter key to exit.")

THIS IS WHAT THE RESULTS LOOKS LIKE WHEN I RUN THE PROGRAM

Welcome to 'Guess My Number'!

I'm thinking of a number between 1 and 100.
You Only Have Five Guesses.

Go Ahead and Take a guess: 99
Guess Lower...
Take Another guess: 98
You guessed it! The number was 85
And it only took you 2 tries!

Guess Lower...
Take Another guess: 44
You guessed it! The number was 85
And it only took you 3 tries!

Guess Higher...
Take Another guess: 55
You guessed it! The number was 85
And it only took you 4 tries!

Guess Higher...
Take Another guess: 33
You guessed it! The number was 85
And it only took you 5 tries!

Sorry You Lose!!!!
The Number was 85
Guess Higher...
Take Another guess:
 
M

Michael Hartl

You need to put the "You guessed it..." block inside a conditional (if
guess == number) so that it only executes if the guess is right. (You
also need to break out of the loop in that case.) As written, the "You
guessed it..." block executes the first time through the loop
regardless of the guess. Also, if the initial guess is right, then the
while guess != number block never executes, and the message is never
displayed.

What you need is an infinite loop that breaks on a correct guess or
upon reaching the maximum number of tries.

Here's a corrected version:

# import random
#
# print "\tWelcome to 'Guess My Number'!"
# print "\nI'm thinking of a number between 1 and 100."
# print "You Only Have Five Guesses.\n"
#
# # set the initial values
# number = random.randrange(100) + 1
# guess = int(raw_input("Go Ahead and Take a guess: "))
# tries = 1
# max_tries = 5
#
# # guessing loop
# while True:
# if guess == number:
# # Guess is right!
# print "You guessed it! The number was", number
# print "And it only took you", tries, "tries!\n"
# break
# elif tries == max_tries:
# print "Sorry You Lose!!!!"
# print "The Number was ", number
# break
# elif guess > number:
# print "Guess Lower..."
# elif guess < number:
# print "Guess Higher..."
# guess = int(raw_input("Take Another guess: "))
# tries += 1
#
# raw_input("\n\nPress the enter key to exit.")

Michael
 
M

Michael Hartl

Sorry about the code: Google Groups screwed up the formatting, but I
hope you get the picture.
 
B

bruno modulix

Chad said:
Hey guys,

Hope you can help me again with another problem. I am trying to learn
Python on my own and need some help with the following.

I am writing a program that lets has the pc pick a number and the user
has five guess to get the number.
1. BUG: If the number is say 35 and I guess 41 the program tells me
that I guessed the correct number and tells me I guessed 31.

2.When I do get the correct number I can not get the program to stop
asking me for the number.


Your help is greatly appreciated.

Chad

# Five Tries to Guess My Number
#
# The computer picks a random number between 1 and 100
# The player gets Five tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
#
# Chad Everett 2/10/2005

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "You Only Have Five Guesses.\n"

# set the initial values
number = random.randrange(100) + 1
guess = int(raw_input("Go Ahead and Take a guess: "))
tries = 1


# guessing loop


while guess != number:

You actually need a second condition : tries >= 5
if (guess > number):
print "Guess Lower..."
else:
print "Guess Higher..."

The fact that guess is not strictly greater than number doesn't mean
it's strictly lower... You should handle 3 cases :
1/ guess > number
2/ guess < number
3/ guess == number

Here the 2/ and /3 are handled by the same branch...
guess = int(raw_input("Take Another guess: "))
tries += 1
print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"

This will be executed whatever ! This code should be *outside* the loop.
(remember that in Python, whitespace is significative...)

(snip)

HTH
 
F

Francis Girard

Mmm. This very much look like a homework from school. Right ?
Francis Girard

Le lundi 14 Février 2005 04:03, Chad Everett a écrit :
 
C

Chad Everett

Nope, I am trying to learn it on my own. I am using the book by Michael
Dawson.

Thanks,
Mmm. This very much look like a homework from school. Right ?
Francis Girard

Le lundi 14 Février 2005 04:03, Chad Everett a écrit :
 
F

Francis Girard

Sorry.
You have my cheers.

I'm suggesting you to use the python debugger through some GUI ("Eric3"
http://www.die-offenbachs.de/detlev/eric3.html for example is a nice and easy
to use GUI). It will greatly help you appreciate Python control flow in
execution and you will learn a lot more trying to find your bugs that way. It
sometimes help to suffer a little. But, of course, I think you will always
find an helping hand if needed.

Regards

Francis Girard

Le lundi 14 Février 2005 21:58, Chad Everett a écrit :
 
S

Scott David Daniels

Francis said:
Mmm. This very much look like a homework from school. Right ?
Francis Girard

Le lundi 14 Février 2005 04:03, Chad Everett a écrit :

It could also be a task from a tutorial page. I'd just make sure
to hand out clues rather than answers, and wait for evidence of work
between clues.

--Scott David Daniels
(e-mail address removed)
 

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,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top