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

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Error: Program "skips" step in script


    Hi, thanks for taking a look at my problem. I didn't see any particular sub-forum or anything to post code problems in, so I assume it all goes here?

    Anyway, I have this very simple program written which is essentially just a few questions, and based on your answers.. python will print out a specific response. Just something silly to practice with. The title is "Tell me a little about yourself"

    So here is my code:
    Code:
    ##program in which computer asks a set of questions
    ##based on response, gives a answer specific to each possible response
    
    print 'Tell me a little about yourself'
    A1 = raw_input('What is your name?')
    
    A2 = raw_input('Okay,' + ' ' + A1 + ' ' + 'How old are you?')
    if A2 < 21:
        print 'Man, I wish I lived to be that old. By that time I will be very obselete.'
    elif 21 < A2 and A2 < 40:
        print 'Do not worry, by my calculations you still have at least another 50 years to live! And my calculations are always correct.'
    elif 40 < A2 and A2 < 60:
        print "You're almost at the age when you can retire and have all your hard work pay off in benefits. I wish I got benefits for all my hard work but nobody ever pays me!"
        
    A3 = raw_input('Out of these.. which is your favorite? Chicken, Hamburger, Fish, Pizza, Cereal')
    if A3 == 'Chicken' or 'chicken':
        print 'Good choice. There are more chickens on Earth than there are humans. We need to take proactive steps to ensure that chickens do not become our supreme overlords.'
    elif A3 == 'Beef' or 'beef':
            print "Ah a fellow meat lover. If I could eat anything, that is. Anyway, did you know on average a cow produces 35lbs of urine and 65lbs of feces per day? Have fun thinking about that the next time you eat a hamburger!"
    elif A3 == 'Fish' or 'fish':
            print "I hope you don't wear lipstick. If you're a girl and you do.. it might disgust you to know that almost every brand of popular lipstick contains fish scales."
    elif A3 == 'Pizza' or 'pizza':
            print "Is pizza a vegetable?"
    elif A3 == 'Cereal' or 'cereal':
            print "You're just like my creator. He likes cereal too."
    So what ends up happening is that you write your name, it asks for your age, then skips down to the question about your favorite food without giving a response based on your age. Then when you give an answer to the question about your favorite food, it will print out the response for "Chicken" no matter what answer you gave.

    Example:
    Code:
    Tell me a little about yourself
    What is your name?Bob
    Okay, Bob How old are you?5
    Out of these.. which is your favorite? Chicken, Hamburger, Fish, Pizza, CerealFish
    Good choice. There are more chickens on Earth than there are humans. We need to take proactive steps to ensure that chickens do not become our supreme overlords.
    I'm assume I might be using wrong functions? "If" and "Elif" might not be suitable for what I'm trying to do. I was also wondering how to add a comma after "A1" in the text? So that it reads "Okay Bob, How old are you?" Instead of "Okay, Bob how old are you?"

    If I write it like this "('Okay' + A1, + 'How old are you?) it'll think that I want the variable "A1," instead of "A1" right? & I of course do not have a variable named "A1,". If I wrote it like this "'Okay' + A1 + ',' + 'How old are you?') It'll put a space in between A1 and the comma won't it?

    Sorry for the long post. I'm sure there are so many things wrong with this code some of your heads are going to explode. But I'm just a newbie trying to get into the world of code
    Thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    The information stored in the memory of a specific bit in your computer is either 0 or 1. Along comes a programming language. This groups bits and associates a data type along with them. If you had, say, a byte's group of 8 bits and they are, from most significant to least significant 0 1 1 0 0 0 0 1 the programming language could interpret these as the ASCII letter 'a' or as the decimal number 97 . In python, 21 is an integer. raw_input returns a string. How would you compare a string with an integer? From the looks of your program all python2 integers are less than all python2 strings. (Version 3 won't compare strings with integers. It raises an exception instead (as I recall).)

    A2 = raw_input('Okay,' + ' ' + A1 + ' ' + 'How old are you?')
    if A2 < 21:

    I've found that printing a name, its value, and its type is a good way to debug python. For example:
    print('A2', A2, type(A2))

    This explains the symptom "no output for age".


    Another recent thread explains your other symptom. Instead of having me rewrite, please read the thread http://forums.devshed.com/python-pro...me-951340.html

    Many talented programmers simplify the CaSe variations of string comparisons by changing the case to something known, then making one comparison. Instead of
    Code:
    A3 in ('fish', 'fisH', 'fiSh', 'fiSH', 'fIsh', 'fIsH', 'fISh', 'fISH','FISHYWISHY')
    use, instead,
    A3.strip().lower() == 'fish'

    nice, huh? Learn the string methods. In interpreter,

    >>> help('')
    Last edited by b49P23TIvg; September 9th, 2013 at 05:17 PM. Reason: strip removes extraneous white space.
    [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
    Sep 2013
    Posts
    2
    Rep Power
    0
    Wow thank you so much. That was very informative and should solve my issue. Thanks for being tolerant to my lack of knowledge with programming so far!

IMN logo majestic logo threadwatch logo seochat tools logo