Problem with excess houses Decimals!

0

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 the separated 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 base, conclude 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 not convert the value that the floats assign to float or int (the values are initially in string so I can separate them one by one and can add them together)

Error line 11

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

    
asked by anonymous 14.10.2018 / 16:30

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 () with base 10: '2.5' p>

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

int (float (myStr))

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

    
14.10.2018 / 17:02