我想通過將整數n
傳遞到字符串格式字段來將pi打印到小數位n
。但是,這給我一個錯誤。什麼是正確的方法來做到這一點?使用整數n並使用字符串格式將pi打印到n位的函數
我的代碼:
from math import pi
def print_pi(n):
print(("%."+str(n)) % pi)
錯誤:
Traceback (most recent call last):
File "1.2.3.py", line 4, in <module>
print_pi(5)
File "1.2.3.py", line 3, in print_pi
print(("%."+str(n)) % pi)
ValueError: incomplete format
說'format'方法是_newer_總是正確的。 – martineau