Hello,
I am running into a small problem of not having a code block not executing
after after a logical operator is true. What am I missing or doing wrong.
Any thoughts or opinions would be greatly appreciated.
The block that isn't being executed follows:
elif (guess == the_number) and (tries < total_attempts):
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
print "The correct answer is: ", the_number
Below is the complete script:
#! /usr/bin/python
# Aurthor: Me
# Purpose: Demonstrates
# Date: April 15, 2008
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
total_attempts = 3
# guessing loop
while (guess != the_number):
if (guess > the_number) and (tries < total_attempts):
print "Lower..."
print "You have...", total_attempts - tries, "left."
print "The correct answer is: ", the_number
elif (guess < the_number) and (tries < total_attempts):
print "Higher..."
print "You have...", total_attempts - tries, "left."
print "The correct answer is: ", the_number
elif (guess == the_number) and (tries < total_attempts):
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
print "The correct answer is: ", the_number
elif (tries >= total_attempts):
print "You're out of guess"
print "You have...", total_attempts - tries, "left."
print "You need more practice."
print "The correct answer is: ", the_number
break
else:
print "You shouldn't see this message..."
print "You have...", total_attempts - tries, "left."
print "The correct answer is: ", the_number
break
guess = int(raw_input("Take a guess: "))
tries += 1
raw_input("\n\nPress the enter key to exit.")
PS: I am new to coding & scripting.
Pete
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
--
http://mail.python.org/mailman/listinfo/python-list