3

I keep getting a syntax error on the while loop, and I'm not understanding why.

def main():
    n=1
    i=1
    flag=True
    num1=eval(input("Enter number")
    while i<9:
        n=n+1
        num2=eval(input("Enter number", n))
        r=r+1
        if num2<num1:
            flag=False
        num1=num2
    if flag==True:
        print("yes")
    else:
        print("no")
main()
4
  • 1
    Your i variable is never incremented (infinite loop). Maybe linked to useless line r=r+1? Commented Oct 25, 2012 at 15:51
  • Paste the exact error you are getting - nothing is aparent on the code above, but you might have mixed tabs and spaces. What editor are you using? Commented Oct 25, 2012 at 15:52
  • BTW, flag==True is a tautology. Just flag is enough. Commented Oct 25, 2012 at 15:54
  • Is there a reason why you're removing all of your code from your questions after they are answered? They make absolutely no sense without the code in them. Also note that this was brought up on Meta. Commented Oct 30, 2012 at 21:56

3 Answers 3

4

Your syntax error is because the expression above the while loop is missing a closed paren:

 num1=eval(input("Enter number")

I'd also reccomend taking your code over to the Code Review SE for constructive feedback on other issues with your code.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd prefer int() or float() over eval.
3
def main():
n=1
i=1
flag=True
num1=eval(input("Enter number"))
while i<9:
    n=n+1
    num2=eval(input("Enter number", n))
    i+=1
    if num2<num1:
        flag=False
    num1=num2
if flag==True:
    print("yes")
else:
    print("no")
main()

You left a parameter open at num1=eval(input("Enter number"))

I also changed r = r + 1 to r+=1, they do the same thing but it reads a little bit nicer.

you can also insure that the number is an integer by changing it to:

num1=int(input("Enter number: "))

Also, I think the n+=1 needs to be i+=1 to end the infinite loop.

Comments

0

Take car of your infinite loop. The final code could be (including other peers good answers):

def main():
    n=1
    flag=True
    num1=eval(input("Enter number"))
    while n<9:
        n+=1
        num2=eval(input("Enter number", n))
        if num2<num1:
            flag=False
        num1=num2

    if flag:
        print("yes")
    else:
        print("no")
main()

Simply loop on variable n: i and r are useless here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.