【发布时间】:2020-02-20 15:05:37
【问题描述】:
我想制作一个程序,它会返回 PI 编号的小数点后第 n 个数字。 只要我不将 n 设置为高于 50,它就可以很好地工作。我可以修复它还是 Python 不允许这样做?
这是我的代码:
pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428
def nth_pi_digit(n=input("How many digits after decimal you would like to see?(PI number): ")):
try:
n = int(n)
if n <= 204:
return print(format(pi, f".{n}f"))
else:
print("Sorry, we have pi with 204 digits after decimal, so we'll print that")
return print(format(pi, ".204f"))
except:
print("You need to put a integer digit")
nth_pi_digit()
【问题讨论】:
-
在默认参数中使用副作用是可怕的伎俩。而且它只适用于函数定义时间,所以这是你代码中的一个错误
-
另外,
return print(...)返回None。这是print(...); return的一个非常奇怪的简写形式 -
这能回答你的问题吗? 1000 digits of pi in Python