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

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Help with if statements


    This is not working right, not sure whats wronge it seems like it would be simple.

    Code:
    #!/usr/bin/python
    #Code by elsheepo
    
    var = raw_input("Enter a number between 1 and 100: ")
    
    if var == 50:
        print( var + " is equal to 50.")
        
    if var > 50:
        print( var + " is greater than 50.")
        
    if var < 50:
        print( var + " is less than 50.")
    When I run this program, and assign a int less than 50 (ex: 1) it prints out:

    Code:
    Enter a number between 1 and 100: 1
    1 is greater than 50.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    The reason is because, var contains 1 as a string:
    Code:
    >>> x = raw_input()
    1
    >>> x
    '1'
    >>> type(x)
    <type 'str'>
    >>>
    You could do the below to convert the raw_input from string to int, but you need to add validation to ensure that the user does not enter a string:
    Code:
    var = int(raw_input("Enter a number between 1 and 100: "))
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    I don't understand, maybe I'm asking the wronge questions.

    1. How do you have the user input an interger (or float) value, and asign it to a variable?

    2. How do you get it to print that variable along with a string? I rewrote the code and I'm getting a different error completly.

    Code:
    #!/usr/bin/python
    #Code by elsheepo
    
    x = int(raw_input("Enter a number between 1 and 100: "))
    
    if x == 50:
        print( x + " is equal to 50.")
        
    if x > 50:
        print( x + " is greater than 50.")
        
    if x < 50:
        print( x + " is less than 50.")
    I get

    Code:
    >>> 
    Enter a number between 1 and 100: 1
    
    Traceback (most recent call last):
      File "/home/earnold/Documents/if statements.py", line 13, in <module>
        print( x + " is less than 50.")
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Lightbulb Solved


    Code:
    # IF statements
    # Code by elsheepo
    x = input("Enter a number: ")
    if x == 50:
        print(str(x) + " is equal to 50.")  # To print an integer, use 'str()'
    if x > 50:                              # within the 'print()'
        print(str(x) + " is greater than 50.")
    if x < 50:
        print(str(x) + " is less than 50.")
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Now rewrite using
    elif statement.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Cool elif statement


    Originally Posted by b49P23TIvg
    Now rewrite using
    elif statement.
    Code:
    # Python 2.6.6
    # elif statements
    # Code by elsheepo
    x = input("Enter a number between 1 and 100: ")
    if x > 0 and x <= 100:
        if x == 50:
            print(str(x) + " is equal to 50.")  # To print an integer, use 'str()'
        if x > 50:                              # within the 'print()'
            print(str(x) + " is greater than 50.")
        if x < 50:
            print(str(x) + " is less than 50.")
    else:
        answer = raw_input("You don't pay much attention do you? (yes or no): ")
        answer.lower()
        if answer == 'yes' or 'y':
            print("Stay in school fool!!")
        elif answer == 'no' or 'n':
            print("Get outa my kitchen!!")
    Hmm, the else statement is acting funny, not getting any errors but when I input...
    Code:
    >>> 
    Enter a number between 1 and 100: -1
    You don't pay much attention do you? (yes or no): n
    Stay in school fool!!
    >>>
    It print's the line as if i entered 'yes' or 'y' ????
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    surprises all around


    I expected
    Code:
        if x > 50:                              # within the 'print()'
            print(str(x) + " is greater than 50.")
        elif x < 50:
            print(str(x) + " is less than 50.")
        else:
            print("50 is equal to 50.")  # To print an integer, use 'str()'
    A non-empty string is always True.
    Code:
    >>> not ''
    True
    >>> not 'n'
    False
    >>>
    Please learn the difference between

    elif (answer == 'no') or 'n':

    and

    elif (answer == 'no') or (answer == 'n'):
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    I see, when using 'or' i need to define the variable in both instances.
    Code:
    elif answer == 'no' or answer == 'n':
    but what's the pourpous of using elif's when adding another if works the same?
    Code:
    elif x < 50:
            print(str(x) + " is less than 50.")
    gives me the same results as
    Code:
    if x < 50:
            print(str(x) + " is less than 50.")
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Clarity! if: elif: elif: else: makes us know that exactly one of the events occurs. Without the else: we know that 0 or one of the events takes place.

    Efficiency! The purpose of elif in this case is efficiency. Python won't need to evaluate so many tests. Can one tell in this dinky program? No. Does it matter if you write commonly used programs with bad algorithms? Yes.


    You could use the in operator.

    answer in set('no n'.split())
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    Not sure I completly understand, but I am pretty new to this codeing stuff. Are you saying that when I put down three 'if''s, as in this case
    Code:
        if x == 50:
            print(str(x) + " is equal to 50.") 
        if x > 50:                              
            print(str(x) + " is greater than 50.")
        if x < 50:
            print(str(x) + " is less than 50.")
    Python actualy checks each one, even if it has already made the determination that x == 50? Where as
    Code:
        if x == 50:
            print(str(x) + " is equal to 50.")
        elif x > 50:                              
            print(str(x) + " is greater than 50.")
        elif x < 50:
            print(str(x) + " is less than 50.")
    would check the first scenario ( x == 50 ) and upon returning a False value immediatly check the second, so on untill it hit a True, and then no further?
  20. #11
  21. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,214
    Rep Power
    1469
    That is correct.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!

IMN logo majestic logo threadwatch logo seochat tools logo