Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newb ??

Reply
Thread Tools

Newb ??

 
 
Chad Everett
Guest
Posts: n/a
 
      11-09-2005
Hi all, I am new to the group. Trying to learn Python programming on my
own. I am working through Michael Dawson's Book Python Programming for the
absolute beginner.

I am tring to write a program that is a number guessing game. I want to be
able to give the user 5 tries to guess the number before the program ends.

I get the result that I want when the user misses after the 5th try. But it
does not go down to my closing statement to end. Rather is askes for
another guess.

I appreciate any help you can give. If this is not the correct group for
these types of questions, I apologize and hope that you can direct me to
another NG.

Thanks,
Chad

import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
print "Sorry you lose."
print "The Correct Number was ", the_number


print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")


 
Reply With Quote
 
 
 
 
mensanator@aol.com
Guest
Posts: n/a
 
      11-09-2005

Chad Everett wrote:
> Hi all, I am new to the group. Trying to learn Python programming on my
> own. I am working through Michael Dawson's Book Python Programming for the
> absolute beginner.
>
> I am tring to write a program that is a number guessing game. I want to be
> able to give the user 5 tries to guess the number before the program ends.
>
> I get the result that I want when the user misses after the 5th try. But it
> does not go down to my closing statement to end. Rather is askes for
> another guess.


Because even after 5 tries, guess != the_number. You won't get past
the while loop until it does. When there have been 5 misses, try
setting
guess=the_number.

>
> I appreciate any help you can give. If this is not the correct group for
> these types of questions, I apologize and hope that you can direct me to
> another NG.
>
> Thanks,
> Chad
>
> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry to Guess it in as few attempts as possible.\n"
>
> #Set the initial values
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess:"))
> tries = 1
>
> #Guessing Loop
>
> while (guess != the_number):
> if (guess >the_number):
> print "Lower..."
> else:
> print "Higher..."
> guess = int(raw_input("Take another Guess:"))
> tries +=1
> if tries == 5:
> print "Sorry you lose."
> print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
>
> raw_input("Press Enter to exit the program")


 
Reply With Quote
 
 
 
 
jmdeschamps@gmail.com
Guest
Posts: n/a
 
      11-09-2005

wrote:
> Chad Everett wrote:
> > Hi all, I am new to the group. Trying to learn Python programming on my
> > own. I am working through Michael Dawson's Book Python Programming for the
> > absolute beginner.
> >
> > I am tring to write a program that is a number guessing game. I want to be
> > able to give the user 5 tries to guess the number before the program ends.
> >
> > I get the result that I want when the user misses after the 5th try. But it
> > does not go down to my closing statement to end. Rather is askes for
> > another guess.

>
> Because even after 5 tries, guess != the_number. You won't get past
> the while loop until it does. When there have been 5 misses, try
> setting
> guess=the_number.
>
> >
> > I appreciate any help you can give. If this is not the correct group for
> > these types of questions, I apologize and hope that you can direct me to
> > another NG.
> >
> > Thanks,
> > Chad
> >
> > import random
> >
> > print "\tWelcome to 'Guess my Number'!"
> > print "\nI'm Thinking of a Number between 1 and 100."
> > print "\nTry to Guess it in as few attempts as possible.\n"
> >
> > #Set the initial values
> > the_number = random.randrange(100) + 1
> > guess = int(raw_input("Take a guess:"))
> > tries = 1
> >
> > #Guessing Loop
> >
> > while (guess != the_number):
> > if (guess >the_number):
> > print "Lower..."
> > else:
> > print "Higher..."
> > guess = int(raw_input("Take another Guess:"))
> > tries +=1
> > if tries == 5:
> > print "Sorry you lose."
> > print "The Correct Number was ", the_number
> >
> >
> > print "You guess correctly!!"
> > print "The number was:", the_number
> > print "You got it correct in", tries, "tries"
> >
> > raw_input("Press Enter to exit the program")


here is your corrected version:
##FROM here it's good
import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
## Here you always get 100, randrange is from start to end+1
the_number = random.randrange(0,101)
##This is OK
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while (guess != the_number):
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1
if tries == 5:
## Missing break to explicitly leave loop after 5
break
## Then test to see if last guess equals the_number
if guess != the_number:
print "Sorry you lose."
print "The Correct Number was ", the_number
else:
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"

raw_input("Press Enter to exit the program")

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      11-09-2005
Chad Everett wrote:
> Hi all, I am new to the group. Trying to learn Python programming on my
> own. I am working through Michael Dawson's Book Python Programming for the
> absolute beginner.
>
> I am tring to write a program that is a number guessing game. I want to be
> able to give the user 5 tries to guess the number before the program ends.
>
> I get the result that I want when the user misses after the 5th try. But it
> does not go down to my closing statement to end. Rather is askes for
> another guess.
>
> I appreciate any help you can give. If this is not the correct group for
> these types of questions, I apologize and hope that you can direct me to
> another NG.
>
> Thanks,
> Chad
>
> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry to Guess it in as few attempts as possible.\n"
>
> #Set the initial values
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess:"))
> tries = 1
>
> #Guessing Loop
>
> while (guess != the_number):
> if (guess >the_number):
> print "Lower..."
> else:
> print "Higher..."
> guess = int(raw_input("Take another Guess:"))
> tries +=1
> if tries == 5:
> print "Sorry you lose."
> print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
>
> raw_input("Press Enter to exit the program")
>
>

I don't personally think the two possible solutions you've been given
are very pythonic. All you really need to do to get the loop termination
conditions right (he says confidently, and with absolutely no testing at
all) is change the while line to

while guess != the_number and tries <=5:

but there are other things wrong with the program. In "pseudo-code"
(stuff that helps you think about the problem without being able to feed
it into an interpreter) your solution should be

while not guessed and guesses < 5
guess number
if guessed
congratulate user
else
crow and rub user's face in it

At present your code will always tell the user she guessed correctly,
even after it's told her she lost. See if this (tested) rewrite makes
sense to you:


import random

print "\tWelcome to 'Guess my Number'!"
print "\nI'm Thinking of a Number between 1 and 100."
print "\nTry to Guess it in as few attempts as possible.\n"

#Set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess:"))
tries = 1

#Guessing Loop

while guess != the_number and tries < 5:
if (guess >the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take another Guess:"))
tries +=1

if guess == the_number:
print "You guess correctly!!"
print "The number was:", the_number
print "You got it correct in", tries, "tries"
else:
print "Sorry you lose."
print "The Correct Number was ", the_number

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

 
Reply With Quote
 
bonono@gmail.com
Guest
Posts: n/a
 
      11-09-2005
I would rather to do the loop as :

max_try = 5
for x in xrange(max_try):
g = int(raw_input(["Take a guess","Take another guess"][x != 0]))
if g == number:
print "bingo, hit it in %i tries" % x + 1
break
elif g < number: print "try higher"
else: print "try lower"
else: print "sorry, you lose, the number is %i" % number

Chad Everett wrote:

> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry to Guess it in as few attempts as possible.\n"
>
> #Set the initial values
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess:"))
> tries = 1
>
> #Guessing Loop
>
> while (guess != the_number):
> if (guess >the_number):
> print "Lower..."
> else:
> print "Higher..."
> guess = int(raw_input("Take another Guess:"))
> tries +=1
> if tries == 5:
> print "Sorry you lose."
> print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
>
> raw_input("Press Enter to exit the program")


 
Reply With Quote
 
jmdeschamps@gmail.com
Guest
Posts: n/a
 
      11-09-2005

## Here you always get 100...

This is false , sorry for the wrong comment on this part, it should
rather be:
## randrange is from start to end-1
the_number = random.randrange(1,101)
JM
(But the rest of my comment seems OK)

 
Reply With Quote
 
Norman Silverstone
Guest
Posts: n/a
 
      11-09-2005
:
>> > Hi all, I am new to the group. Trying to learn Python programming on my
>> > own. I am working through Michael Dawson's Book Python Programming for the
>> > absolute beginner.
>> >
>> > I am tring to write a program that is a number guessing game. I want to be
>> > able to give the user 5 tries to guess the number before the program ends.


< snip >

I am also working my way slowly through this book which I find quite
challenging. What I am thinking about at the moment is how to program for
the computer to guess the number which I select. I know it can be done but
I suspect it requires an approach which I have not yet learnt. I would
welcome suggestions on the best way to approach this problem.

Norman
 
Reply With Quote
 
Chad Everett
Guest
Posts: n/a
 
      11-10-2005
Hey guys,

Thanks for your help. I am glad to know there is a community out there
willing to put the effort out to help us newbies. I will work on it. Some
of the suggestions are things that I have not covered yet but I am sure I
will figure it out.

Thanks again.

I am sure I will be back with more questions before too long.

Chad


"Chad Everett" <> wrote in message
news:mzdcf.470$...
> Hi all, I am new to the group. Trying to learn Python programming on my
> own. I am working through Michael Dawson's Book Python Programming for
> the absolute beginner.
>
> I am tring to write a program that is a number guessing game. I want to
> be able to give the user 5 tries to guess the number before the program
> ends.
>
> I get the result that I want when the user misses after the 5th try. But
> it does not go down to my closing statement to end. Rather is askes for
> another guess.
>
> I appreciate any help you can give. If this is not the correct group for
> these types of questions, I apologize and hope that you can direct me to
> another NG.
>
> Thanks,
> Chad
>
> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry to Guess it in as few attempts as possible.\n"
>
> #Set the initial values
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess:"))
> tries = 1
>
> #Guessing Loop
>
> while (guess != the_number):
> if (guess >the_number):
> print "Lower..."
> else:
> print "Higher..."
> guess = int(raw_input("Take another Guess:"))
> tries +=1
> if tries == 5:
> print "Sorry you lose."
> print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
>
> raw_input("Press Enter to exit the program")
>



 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      11-10-2005
Norman Silverstone <> wrote:
...
> challenging. What I am thinking about at the moment is how to program for
> the computer to guess the number which I select. I know it can be done but
> I suspect it requires an approach which I have not yet learnt. I would
> welcome suggestions on the best way to approach this problem.


I assume the way the computer is going to guess is by trying some
number, and you respond either that it's guessed right, or to go lower,
or to go higher.

In that case, think of "bisection". Originally, all the computer knows
is that the number is in some range, say 0 to 100. It can then guess
the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
each case the range was just halved (actually, a bit more than halved).

It does not take many halvings to squeeze even a pretty large original
range down to a range which only includes one possible number... that's
because "2 to the power of N" grows VERY fast with N, as the old story
about the inventor of chess shows (the king told him to ask for a
reward, and all he wanted was some rice -- one grain on the first cell
of the chessboard, two on the next, then four, eight... all the way
through the 64 squares of the board; the kind thought the inventor was
being very moderate in his request, and granted it... to soon find out
that not enough rice existed in the world to satisfy the request....

Well, repeated halving is just like repeated doubling "backwards", so it
squeezes vast ranges of possibilities down to tiny ones just as fast as
repeated doubling produces oceans of rice from a small chessboard.


Alex
 
Reply With Quote
 
Norman Silverstone
Guest
Posts: n/a
 
      11-10-2005
< snip>

> I assume the way the computer is going to guess is by trying some
> number, and you respond either that it's guessed right, or to go lower,
> or to go higher.


Yes, that is correct.
>
> In that case, think of "bisection". Originally, all the computer knows
> is that the number is in some range, say 0 to 100. It can then guess
> the midpoint, 50. If it's right, yay! Otherwise: if it's told to go
> lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
> each case the range was just halved (actually, a bit more than halved).


Thank you, I thought that might be the case. So, I will have to settle
down and try to write some pseudo-code first. I hope to be back.

Norman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX 535: port forwarding newb problem douglas.j.watt@googlemail.com Cisco 1 03-08-2006 12:49 PM
Cisco Newb Question- sorry indo Cisco 7 05-19-2005 04:20 AM
newb: generic vector Pela VHDL 1 01-22-2005 07:07 PM
Newb: Help with code ! Prime VHDL 2 01-02-2005 06:38 PM
Perl newb in need of help with script. David K. Worman Perl 2 09-18-2003 05:07 PM



Advertisments