If Statement1 is true and Statement2 is true

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    If Statement1 is true and Statement2 is true

    Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like
    if (5 > 4 && 3>4) {
    cout << "Hello, world!"
    }
    or in JavaScript
    if (5>4 && 3>4) {
    document.write( "Hello, world!")

    So can you do the same thing in Python? Thanks :-)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Kid Programmer
    Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like
    if (5 > 4 && 3>4) {
    cout << "Hello, world!"
    }
    or in JavaScript
    if (5>4 && 3>4) {
    document.write( "Hello, world!")

    So can you do the same thing in Python? Thanks :-)
    Yes.[code=Python]>>> a = 3
    >>> b = 4
    >>> if a < 5 and b < 5:
    ... print 'Both conditions are True'
    ...
    Both conditions are True
    >>> if a < 5:
    ... if b > 5:
    ... print 'b is greater than 5'
    ... elif b < 5:
    ... print 'b is less than 5'
    ... else:
    ... print 'b equals 5'
    ...
    b is less than 5
    >>> [/code]

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Thanks I tried that and it worked. And you could do it with 3, 4 etc right?

      Comment

      • Smygis
        New Member
        • Jun 2007
        • 126

        #4
        Originally posted by Kid Programmer
        Hello guys. I was wondering if it is possible to execute certain commands within a if statement if two conditions are true. I know that in C++ and JavaScript you can do something like
        if (5 > 4 && 3>4) {
        cout << "Hello, world!"
        }
        or in JavaScript
        if (5>4 && 3>4) {
        document.write( "Hello, world!")

        So can you do the same thing in Python? Thanks :-)
        Last time i did any math 3 whas in fact smaler than 4. Wich makes that statement false. And the hello, world part wont be executed. So if you did the same thing in python im not suprised it didnt work.

        [code=python]
        >>> 3>4
        False
        >>> 3<4
        True
        >>> 5>4 and 3>4
        False
        >>> 5>4 and 3<4
        True
        >>>
        [/code]

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          I was just giving an example.

          Comment

          Working...