Logical Operator and code block not executing (newbie question)

Ben Kaplan bskaplan14 at yahoo.com
Thu Apr 17 03:13:03 CEST 2008


The problem is that your loop says "while guess != number". When guess is equal to the number, the code in the loop is not executed. Instead, do something like

while guess != number and tries < total_attempts:
   if guess > number:
      ...
   elif guess < number:
      ...

if guess == number :
   ...

----- Original Message ----
From: python newbie <p.newbie at yahoo.com>
To: python-list at python.org
Sent: Wednesday, April 16, 2008 8:58:10 PM
Subject: Logical Operator and code block not executing (newbie  question)

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.




      Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.





      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080416/9066ef93/attachment.html>


More information about the Python-list mailing list