Python Forum

Full Version: I'm getting a syntax error very early on in my code and I can't quite figure it out.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my freshman year in college and I had to take a python course. It hasn't been to easy with all reote classes. the goal of this program is for the user to input two agess of dogs in human years and to get back their ages and a specified response.
this is my code so far:
humanage = int(input("please enter your dog's age in human years")
            if humanage <= 2:
             dogage = humanage * 10.5
            else:
               dogage = 21 + (humanage - 2)*4

humanage2 = int(input("please enter your other dog's age in human years")
                if humanage2 <= 2:
                dogage2 = humanage * 10.5
            else:
                dogage = 21 + (humanage2 - 2)*4
        print("your first dog's age in dog years is", dogage)
        print("your second dog's age in dog years is", dogage2)
    if dogage == dogage2:
                print("friends from birth")
            elif dogage > dogage2 or dogage > 50:
                print("senior")
            elif dogage > 35 and dogage2 > 35:
                print("they could be president")
            elif dogage2 < 2:
                print("it's a puppy")
            else:
                print("cats are better anyways")
If anyone could help I would very much appreciate it!
Indentation means something in Python. The indentation of a line of code determines the "depth" of the statement. Indentation is not used to make the program look pretty (though proper indentation does look nice).
if (x == 5):
    x -= 1
    elif (x == 7):  # Indentation error
        x += 1

# Should line up like this
if (x == 5):
    x -= 1
elif (x == 7):
    x += 1
The if and elif need to be indented the same amount otherwise it is like this C equivalent
if (x== 5)
{   x -= 1;
    else if (x == 7)
    {
       x += 1;
    }
}
You have other indentation problems. Your entire program is one indentation problem after another.
humanage = int(input("please enter your dog's age in human years")
if humanage <= 2:  # This is not inside an if, while or for, so it is same depth as line above
    dogage = humanage * 10.5  # This statement is 1 level deep
else:
    dogage = 21 + (humanage - 2)*4  # 4 spaces is the "standard" for indentation
 
humanage2 = int(input("please enter your other dog's age in human years")
if humanage2 <= 2:
    dogage2 = humanage * 10.5
else:
    dogage = 21 + (humanage2 - 2)*4

print("your first dog's age in dog years is", dogage)
print("your second dog's age in dog years is", dogage2)
if dogage == dogage2:
    print("friends from birth")
elif dogage > dogage2 or dogage > 50:
    print("senior")
elif dogage > 35 and dogage2 > 35:
    print("they could be president")
elif dogage2 < 2:
    print("it's a puppy")
else:
    print("cats are better anyways")
That should fix the indentation errors. There may be other syntax errors or logic errors