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())
>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":
...