samedi 17 octobre 2020

Why am I getting a syntax error because of the colon at the end of my if statement?

I am trying to make a program in python that calculates the age of the user with only the input of their birthday, and I'm getting a syntax error for the strangest reason. I used the date.time() function with this code:

today = date.today()
today = today.strftime("%m-%d-%Y")
todaysplit = today.split("-")

After getting the birthday of the user, I made a for loop that saves the year, month, and day of the user. It's working fine up until the 'year' portion. Here is the code that saves the year:

if n == 2:
    arglen = len(arg)
    if int(arg) < 100:
      if int(arg) > int(todaysplit[2]):
        year = str(int("19"+str(arg))
      if int(arg) < int(todaysplit[2]):
        year = str(int("20"+str(arg))
    else:
      if arglen == 4:
        year = int(arg)

The error is this:

File "main.py", line 206
    if int(arg) < int(todaysplit[2]):
                                    ^
SyntaxError: invalid syntax

I have double-checked and triple-checked my code, and there shouldn't be a syntax error. The strangest thing, however, is that whenever I delete the colon, (since that is what it's saying there is an error for,) the error is this:

File "main.py", line 207
    year = str(int("20"+str(arg))
    ^
SyntaxError: invalid syntax

This makes even less sense. I'm not sure how to fix this because I'm not sure why there is even a problem. Can someone help me out please?

Aucun commentaire:

Enregistrer un commentaire