Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 651
user1726993 Avatar asked Jul 24 '26 23:07

user1726993


2 Answers

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.

like image 192
Doug T. Avatar answered Jul 27 '26 11:07

Doug T.


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.

like image 38
Pichu Avatar answered Jul 27 '26 13:07

Pichu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!