Python Forum
I'm getting a syntax error very early on in my code and I can't quite figure it out.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm getting a syntax error very early on in my code and I can't quite figure it out.
#1
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!
Larz60+ write Oct-30-2020, 02:34 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Added for you this time. Please use tags in future posts.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,335 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 2,014 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,980 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,431 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,178 Feb-10-2020, 01:57 AM
Last Post: sbabu
  syntax error: can't assign to operator liam 3 4,059 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,289 Jan-10-2020, 02:30 AM
Last Post: ichabod801
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,446 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,086 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  syntax error in an if statement at the else ? Just_started 2 2,249 Mar-07-2019, 08:35 PM
Last Post: Just_started

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020