2015-10-21 29 views
0

當我嘗試提高一個浮動數字(PE:0.1)的權力的任何東西,腳本創建一個複雜的數字。浮動的東西創建一個複雜的數字 - Python

# -*- coding: utf-8 -*- 

precision = float(input('Introduce the precision that you want for the pi number: ')) 

i = precision 
dif = precision 
previous = 0 

while dif >= precision: 
    my_pi = ((-1)**i)/(2*i+1) 
    res = previous + my_pi 
    dif = abs(res) - anterior 
    anterior = res 
    i+= precision 

print('The final approximation of pi is {0:.6f}'.format(resultado*4)) 

但「第一輪」崩潰給我這個錯誤

Traceback (most recent call last): 
    File "/home/al343729/VJ1203/Prácticas/Práctica 2/Ejer07.py", line 14, in <module> 
    while dif >= precision: 
TypeError: unorderable types: complex() >= float() 

這不以發生FOR循環

+0

這是幹什麼用的? '(-1)** i' – roymustang86

+0

你認爲將-1提升爲一個分數的功率可以做什麼? – user2357112

+0

@ user2357112那麼爲什麼發生在我身上,而不是一個朋友與完全相同的操作? – Doruko

回答

0

您需要在的開頭導入數學後程序,然後使用math.pow()而不是()**()。我之前有同樣的問題,但現在它可以工作。在分配它們之前,您還使用了anteriorresultado

相關問題