Problem with excess Decimals!

Asked

Viewed 122 times

-1

Initially I was developing a python code that would help me better understand Nikola Tesla’s number theory(video).

Explaining quickly, starting with 360, adding each separate value results in 9, for example :3 + 6 + 0 = 9 , always dividing the value 360 by 2 , adding up the separate numbers always results in 9, for example: 360/2= 180, 1+8+0 = 9 180/2 = 90 9+0 = 9 And so on

Initially the base code I made was this in python

real_valor=360 ; string_valor=str(real_valor); valor_unitario=[]; soma_unitaria=0
for i in string_valor:
    if i == '.':
        continue
valor_unitario.append(int(i))
print(valor_unitario)
for g in valor_unitario:
    soma_unitaria += g
print(soma_unitaria)

Using the first as a basis, concludes this one :

soma_unitaria=0; from time import sleep
while True:
    sleep(0.5)
    if soma_unitaria == 0:
        real_valor = 360; string_valor = str(real_valor); valor_unitario = []
    else:
        string_valor='';soma_unitaria = 0; real_valor /= 2; string_valor = (str(real_valor)); valor_unitario.clear()
    for numero in string_valor:
        if numero == '.':
            continue
        valor_unitario.append(int(numero))
    for valor in valor_unitario:
        soma_unitaria += valor
    print("Valor Inicial : {}\nSoma dos valores unitarios divido : {}\n".format(real_valor, soma_unitaria))

The code Works initially. The problem starts after many decimal places, I can’t convert the value floats assign to float or int number (the values are initially string so I can separate them one by one and add them together)

Line error 11

inserir a descrição da imagem aqui

How can I solve this problem, many decimal places end up adding the value and , which in strings I cannot convert to numbers?

1 answer

0

There is a difference between Python2 and Python3 that leads to this error:

With Python2, for example, int(str(5/2)) provides 2. With Python3, the same happens to you: Valueerror: invalid literal for int() based 10: '2.5'

If you need to convert some string that can contain float instead of int, you can use the following formula :

int (float (myStr))

How float ('3.0') and float ('3') provide 3.0, but int ('3.0') gives the error.

  • I’m taking each isolated value, for example 3.14, I’m storing in lists [1.3.4] and adding up the values separately 3+1+4, the problem is that there is a limit of decimals, where nummeros are added and letters are added and it is not possible to convert these letters to int or floats, my doubt is whether there is any way to remove that limit that python instipula

Browser other questions tagged

You are not signed in. Login or sign up in order to post.