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

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Correct syntax compiles as incorrect: Nested If loop


    Running on Raspberry Pi, NOOBS 1.2.1, Python 3.2.

    Code:
    #Player one input
    in1=0
    while in1 > 9 or in1 <= 0:
        in1=int(input("Enter a number between 1 and 9"))
        if in1 >= 1 and in1 <=9:    #This is where the problem is, the compiler states that the colon denoting the start of the if block is incorrect. Taking this away creates a syntax error with the following print command.
            print("Happy")
            break
        else:
            print("Enter a valid number")
    Note the result of print("Happy") is not actually the end result of the code, it sets off a series of predefined functions, but Happy is just a placeholder to make it easier to understand.

    Any help would be greatly appreciated,
    Nootz
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    156
    Rep Power
    2
    Originally Posted by Nootz
    Running on Raspberry Pi, NOOBS 1.2.1, Python 3.2.

    Code:
    #Player one input
    in1=0
    while in1 > 9 and in1 <= 0:
        in1=int(input("Enter a number between 1 and 9"))
        if in1 >= 1 and in1 <=9:    #This is where the problem is, the compiler states that the colon denoting the start of the if block is incorrect. Taking this away creates a syntax error with the following print command.
            print("Happy")
            break
        else:
            print("Enter a valid number")
    Note the result of print("Happy") is not actually the end result of the code, it sets off a series of predefined functions, but Happy is just a placeholder to make it easier to understand.

    Any help would be greatly appreciated,
    Nootz
    The parser may be confused. Try either
    if (in1 >= 1) and (in1 <=9):

    or
    if 1<=in1<=9:
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    The syntax of the code you posted is valid. If you remove the colon at the end of the while or if statement the syntax becomes invalid. http://docs.python.org/3/reference/index.html defines python syntax.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by rrashkin
    The parser may be confused. Try either
    if (in1 >= 1) and (in1 <=9):

    or
    if 1<=in1<=9:
    Thank you very much for the solutions, but having tried both of them out, neither of them work. They both come back with the exact same error

    @b49P23TIvg, I realise this, but the syntax should be correct, hence why I posted this thread...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    My first guess is a context issue. Is that all there is to the code? See what your snippet is nested in and if you skipped a colon there.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    Code:
    while in1 > 9 and in1 <= 0:
    I think you probably mean "or" here as in1 can not be >9 and <=0 so the code posted will never get to the following statements. And note that the while statement is not the same as
    Code:
    if in1 >= 1 and in1 <=9:
    Last edited by dwblas; July 31st, 2013 at 12:20 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Mr909
    My first guess is a context issue. Is that all there is to the code? See what your snippet is nested in and if you skipped a colon there.
    The start of this snippet has no previous indents. It starts at the leftmost part of the screen, so it nesting in something else is not a problem.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by dwblas
    Code:
    while in1 > 9 and in1 <= 0:
    I think you probably mean "or" here as in1 can not be >9 and <=0 so the code posted will never get to the following statements. And note that the while statement is not the same as
    Code:
    if in1 >= 1 and in1 <=9:
    Ah, that was a typo in translating to this forum. The code should be "or", I've updated that now.

    Yes, I realise that using a "while" statement is different; I want the statement to loop continuously until a valid number is entered by the user.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Maybe the premise is wrong. What the heck is an "if loop"?
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    This usually means you missed a close-paren somewhere. I don't see any missing in that code snippet, but did you skip something?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Maybe the premise is wrong. What the heck is an "if loop"?
    I suppose I worded it badly, I used the "while" statement to create a loop until the "if" statement is fulfilled, in which case the "while" loop is also fulfilled, and the program continues.

IMN logo majestic logo threadwatch logo seochat tools logo