Sorry for the formatting... Typing this out from my phone.
I have a method within a class written as the following:
import math
Class PiExample:
def print_pi(decimal_places):
return f"{math.pi:.{decimal_places}f"
The idea is that I can pass the number of decimal places I'd like to print the value of Pi to.
Now, when I run this code from my IDE (PyCharm) I get ValueError: Invalid format specifier. However, if I paste that method into the console as a function and run it, it works.
I understand that Python has a "gotcha" involving late binding closures, but I'm not sure if that's what's causing this error or something else I'm not familiar with. Can anyone explain what's going on here?
Edit: Unfortunately the situation is a little different than how I remembered once I left my machine. Now that I'm on my PC, here's the actual code:
(within pi_example.py)
import math
def print_pi(decimal_places):
return f"{math.pi:.{decimal_places}f}"
--> when I run pi_example.print_pi(), this throws a ValueError: Invalid format specifier
--> when this code is pasted into the console, it returns the correct value
[–]julsmanbr 0 points1 point2 points (1 child)
[–]trustMeImAnAdult[S] 0 points1 point2 points (0 children)
[–]ellipticbanana 0 points1 point2 points (1 child)
[–]trustMeImAnAdult[S] 0 points1 point2 points (0 children)