0

My Error
Python throws a syntax error pointed at the last "e" of "else:", preceded by an if statement and inside a while loop.
My Objective
Test if certain parameters are true, if true then go to the beginning of the loop and if not true then perform certain statements and increment a value.
My Source Code

from random import randint

def returnDigRoot(num):
    digs = []
    while len(str(num)) != 1:
        num = str(num)
        for each in num:
            digs.append(each)
        num = int(num)
        digs = [int(i) for i in digs]
        num = sum(digs)
    return(num)
def rnum():
    return(randint(1,99999))
ran_nums = []
sols = []
it = 1

The problem area is here

while it <= 3:
    print("Generating numbers")
    current = randint(1,99999)
    print("randomly intializing the 'current' int value")
    print("testing if the digital root is greater than 6")
    if returnDigRoot(current) > 6:
        print("going back to start of loop")
        continue
    print("testing if it isnt")
    else:
        ran_nums.append(current)
        print("append 'current' to ran_nums")
        sols.append(returnDigRoot(current))
        print("appending its digital root to sols")
        it += 1
        print("incrementing the iterator variable")

My Research
I looked at many questions on StackOverflow and other sites and could not find a solution to my problem; most problems people had with else statements were related to tabbing errors, preceding errors (which I checked for), no preceding if statement, or multiple else statements.

Thanks in advance for any help.

7
  • 1
    Please cut/paste the traceback you see and the command you use to execute the program. Commented Nov 28, 2015 at 17:52
  • code seems to have issues. use prints to find your error Commented Nov 28, 2015 at 17:57
  • I ran the code from IDLE, so it doesn't have a traceback, it just shows a popup box that says "syntax error", and the print statements i had in before removing them from the question don't come up, it's just the popup box Commented Nov 28, 2015 at 17:58
  • The code works, but it works forever Commented Nov 28, 2015 at 17:58
  • Confusing syntax errors are often due to missing closing parentheses. The syntax error is triggered by code that is fine, it's just the first thing that would not be valid inside the previous parenthesized expression. I don't see any such issue in the code you've posted, but perhaps you've fixed the error in copying your code to Stack Overflow. Commented Nov 28, 2015 at 18:03

4 Answers 4

4

print("testing if it isnt") needs to be indented. As it stands, your code doesn’t really relate the if with the else because of the indentation. It’s like writing something like this in C:

if(<condition>)
{
  <action>
}
prinf(...)
else
{
 <action>
}

Just align the print line with the rest of the code under the if statement.

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

Comments

1

The line:

print("testing if it isnt")

isn't indented correctly. You can't have anything in between an if block and the else block.

Comments

0

Your statement:

print("testing if it isnt")

is indented to the wrong level; that makes the else: that follows an independent statement, which is syntactically wrong. Probably you meant that print statement to follow the else and be indented one level.

Comments

0

This is most likely a indentation/space/tabbing issue since I copy pasted the code and I don't get any errors. Though I am on Python 2.7.10. (Repasting it here to ensure you can copy paste the same and try):

from random import randint

def returnDigRoot(num):
    digs = []
    while len(str(num)) != 1:
        num = str(num)
        for each in num:
            digs.append(each)
        num = int(num)
        digs = [int(i) for i in digs]
        num = sum(digs)

    return(num)
def rnum():
    return(randint(1,99999))
ran_nums = []
sols = []
it = 1

while it <= 3:
    current = randint(1,99999)
    if returnDigRoot(current) > 6:
        continue
    else: # this is where the error is pointed
        ran_nums.append(current)
        sols.append(returnDigRoot(current))
        it += 1

On an unrelated note, that while loop will take a long time to exit since the exit criteria is very small (two current <=36 only will cause exit).

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.