我試圖編寫一個返回golden ratio到小數點後限制的程序。 我到目前爲止是這樣的:根據python中的變量限制小數點
def gr(n):
a, b = 0, 1
for x in range(0, n):
a, b = b, a+b
else:
return(a)
decimals = int(input("What amount of decimals do you want to see of the golden ratio?"))
ratio = gr(41)/gr(40)
print(format(ratio, '2f'))
的問題是,我不能找到一種方法decimals
格式化ratio
的號碼。
既然你沒有''break'在'for'循環中,我不太確定'else'是否需要在這裏。 – IanAuld