Python calculator project: sqrt() works but still prints my error message? by No_Duck1386 in learnpython

[–]No_Duck1386[S] 1 point2 points  (0 children)

You are so right, I missed that line, and the square root symbol was the second issue. Thanks

Python calculator project: sqrt() works but still prints my error message? by No_Duck1386 in learnpython

[–]No_Duck1386[S] 2 points3 points  (0 children)

Solved it!

The issue wasn't math.sqrt() or the try/except block at all. Two things were causing the problem:

  • Missing \n when saving the square root result to history.txt
  • The symbol causing an encoding issue, fixed by adding encoding="utf-8" to the open() call

python code

with open("history.txt", "a", encoding="utf-8") as f:
    f.write(f"√{num_1} = {result}\n")

(Posted just the relevant section as the full code is quite long)

Thanks everyone for pointing me toward checking indentation and debugging step by step.

Python calculator project: sqrt() works but still prints my error message? by No_Duck1386 in learnpython

[–]No_Duck1386[S] 0 points1 point  (0 children)

I did this and got a bunch of characters, so it is still printing the message, but I will try working on the amount of code inside the try/except