user1726993
user1726993

Reputation: 43

Getting a syntax error on a while loop

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()

Upvotes: 3

Views: 17812

Answers (3)

Askolein
Askolein

Reputation: 3378

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.

Upvotes: 0

Pichu
Pichu

Reputation: 275

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.

Upvotes: 3

Doug T.
Doug T.

Reputation: 65639

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.

Upvotes: 4

Related Questions