Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SyntaxError: expected ':' despite having a colon in the specified place

I’ve been trying to make a small journal program, and whenever I try to run it, the program errors out with

SyntaxError: expected ':'

The program should run as normal, allowing the user to write into their journal. What happens is that the code quits out saying there’s a syntax error on the else statement. I’ve tried to rewrite the code to check if I’ve missed something, but everything seems correct.

currentDate = datetime.today().strftime('%Y-%m-%d')


f = open(currentDate, "w")
    print("Write what you want to add into your journal, and it'll be saved when you hit enter.")
    f.write(input())
    f.close()
    print("Are you done? (press 'y' if you are)")
    userfinished = input()
    if userfinished == "y":
        print("Alright then.")
        time.sleep(1)
        print("You can now read the file you've created in your operating system's file explorer (in built reader coming soon).")
        time.sleep(1)
        print("Goodbye.")
        sys.exit()
    else userfinished == "n":
        print("Alright, keep on writing.")
        f = open(currentDate, "a")
        f.write(input())

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s a syntax error on your else statement. For else there’s no condition to be met because it implies ALL other conditions not specified in previous ifs.

You could do

if userfinished == "y":
    ...
else: 
    ...

or,

if userfinished == "y":
    ...
elif if userfinished == "n":
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading