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

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Just a quick Question


    So I am trying to do a conditional statement using boolean logic my code is

    Rock = True

    Goth = False

    Pop = True


    music = input ("what is your fav music ")

    if music == True:

    print ("That is Good Music")

    else:
    print (" No that sucks")



    However when I run this script every time I input Rock , or I input Pop the programs prints the function assigned to else

    what am I doing wrong?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    The answer depends upon whether you're using python 3 or an earlier version.

    In python 3 input returns a string which is never equal to the Boolean data type value True and so the else event always happens.

    In python 2 input evaluates the string you type, and returns that. Thus, in python2 the entries of
    True
    or
    1
    would cause the true branch of the if statement to execute.

    (or if you happen to have a variable named a with the value of 1 assigned to it you could enter a as well.)

    [edit] false sentence removed [/edit]
    Last edited by b49P23TIvg; September 24th, 2012 at 05:10 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    Originally Posted by b49P23TIvg
    The answer depends upon whether you're using python 3 or an earlier version.

    In python 3 input returns a string which is never equal to the Boolean data type value True and so the else event always happens.

    In python 2 input evaluates the string you type, and returns that. Thus, in python2 the entries of
    True
    or
    1
    would cause the true branch of the if statement to execute.

    (or if you happen to have a variable named a with the value of 1 assigned to it you could enter a as well.)

    Study the tutorials a while longer, you're quite near the clueless level at which we all begin.
    alright thanks two quick things 1) I got a book called python 3 for absolute beginners is this good for learning and 2) so I would have to convert the string into a numerical data string right

    Comments on this post

    • b49P23TIvg agrees : I apologize for earlier negative remarks.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Yes, exactly.

    Oh, you are using python3. Reading this with a python3 point of view I've inserted the error trapped eval statement
    Code:
    Rock = True
    Goth = False 
    Pop = True
    string = input ("what is your fav music ")
    
    try:
        music = eval(string)
    except:
        music = False
    
    if music == True:
        print ("That is Good Music") 
    else: 
        print (" No that sucks")
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Smile


    Thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    Originally Posted by Nick Wyden
    Thanks

    OHHHHH ok so its illogical to try to do a else statement in that

    context. This forum is honestly just so helpful
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    How have you made four threads and still not know how to use code tags? Put your code inside [code][/code] or use the '#' button in the editor.

IMN logo majestic logo threadwatch logo seochat tools logo