FAQ
stuck on python for absolute beginners
chapter 6
i actually done what i was supposed to do use the function ask_number for guess a number
but for some reason it does not count correctly the number of tries

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try 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

while (guess != the_number):

if (guess > the_number):
print "Lower..."

else:
print "Higher..."
tries += 1

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

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

raw_input("\n\nPress the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...

## Search Discussions

• at Aug 7, 2008 at 6:14 pm ⇧ delete the extra 'tries += 1' after
else:
print "Higher..."
tries += 1 #delete this

while at it, and add this line as the first line in function ask_number()
global the_number, tries

good luck.
Edwin

-----Original Message-----
From: python-list-bounces+edwin.madari=verizonwireless.com at python.org [mailto:python-list-bounces+edwin.madari=verizonwireless.com at python.org]On Behalf Of garywood
Sent: Thursday, August 07, 2008 1:56 PM
To: python-list at python.org
Subject: very newbie question

stuck on python for absolute beginners
chapter 6
i actually done what i was supposed to do use the function ask_number for guess a number
but for some reason it does not count correctly the number of tries

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try 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

while (guess != the_number):

if (guess > the_number):
print "Lower..."

else:
print "Higher..."
tries += 1

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

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

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

The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080807/8d110661/attachment.htm>
• at Aug 7, 2008 at 7:14 pm ⇧ garywood wrote:
stuck on python for absolute beginners
chapter 6
i actually done what i was supposed to do use the function ask_number
for guess a number
but for some reason it does not count correctly the number of tries

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try 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

while (guess != the_number):

if (guess > the_number):
print "Lower..."

else:
print "Higher..."
tries += 1
^-- *not needed*

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

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

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

------------------------------------------------------------------------
~Ethan~
• at Aug 8, 2008 at 5:58 am ⇧ Try this:

# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
tries = 0

guess = int(raw_input("Take a guess: "))
tries = 1

while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
tries += 1
guess = int(raw_input("Take a guess: "))
tries += 1

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

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

The variables "the_number" and "tries" were not available outside the

Alternatively drop the def function and lump it all into a simple script.
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things?Niccolo Machiavelli, /The Prince/,
ch. 6

## Related Discussions

 view thread | post
Discussion Overview
 group python-list categories python posted Aug 7, '08 at 5:55p active Aug 8, '08 at 5:58a posts 4 users 4 website python.org

### 4 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase