#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Extremely basic code help


    I am just starting to program, about a day in and I wrote up this little code using the stuff I have learned so far. The code has no errors but it does not work properly and I can not seem to understand why. I want the code to give the logical results for the answers put into it. I am using 2.7

    Code:
    print 'Is that an animal'
    thing = raw_input('')
    print 'What kind?'
    animal = raw_input('')
    
    if thing == 'yes':
        if animal == 'monkey':
            print 'Oh wow!'
        else:
            print 'Never heard of it'
    
    else:
        print 'Get it outta here then!'
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Please post the dialogue you're hoping to see.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    I'm just trying to get the text to print if certain conditions are met. So, if the program asks "Is that an animal?", and the raw_input is "yes", then the program will reply "What kind?". If the raw_input is anything but "yes", then the program will reply
    "get it outta' here then!". If the raw_input to "What kind?" is "monkey', then the program will reply "Oh, wow!". If the raw_input is anything but "monkey" then the program will reply "Never heard of it".
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Thank you for the clear description.


    Here's one way
    Code:
    def monkeys():
        if 'yes' != raw_input('Is that an animal?').lower():
            return "get it outta' here then!"
        if 'monkey' == raw_input('What kind?').lower():
            return 'Oh, wow!'
        return 'Never heard of it'
    
    if '__main__' == __name__:
        print monkeys()
    else:
        print 'use monkeys() function in this module'
    Here's another way to write the logic in a style similar to your post. The essential change is that I've moved the "What kind?" question into one side of the test. The question arises depending on the first question.
    Code:
    if not ('yes' == raw_input('Is that an animal?').lower()):
        print "get it outta' here then!"
    else:
        if 'monkey' == raw_input('What kind?').lower():
            print 'Oh, wow!'
        else:
            print 'Never heard of it'

    *Oh, I used a negative test for stylistic reasons.
    Code:
    # I rewrite
    if condition:        # poor style.  "if" is physically far from "else"
        many
        lines
        of
        code
    else:
        fewer lines of code
    
    
    # as
    if not (condition):    # put "else" close to "if"
        fewer lines of code
    else:
        many
        lines
        of
        code

    **Oh, some people swear that a function should have one return. (Dykstra, in particular as I recall.) I happen to prefer short functions with few indentation levels. Multiple return statements don't scare me. Now the comefrom statement---that's another beast.
    Last edited by b49P23TIvg; September 1st, 2012 at 10:13 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo