[Python] Please resolve Python "magic" in this calculation [closed]

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I created a script that should perform simple math juggling by rearranging numbers.

    What it should do:

    x = 777.0
    y = 5
    calc = x / y # 155.4


    Pseudocode:

    Rearrange numbers (last digit + first) = 555.
    Difference from 777 and 555 = 222
    Add 222 to 555 = 777


    Basically it should recreate the original variable without doing a real calculation but instead just rearrange numbers and add.

    Because of the design of the script I expected it to work only with 4 digit numbers like 333.3. It turns out that it (seems to) work also with numbers like 2543.6452 which seems to be impossible at least from my (non academic) view. Can someone please tell me what happens here? Is the code working correctly or did I create something I simply don't understand? It looks like a illusion to me.

    x = 5.0
    y = 7345.3297
    z= y / x
    print "Initial Value = " + str(y)
    print "Calculate:"
    print str(y) + "/" + str(x)
    print z # 177.6
    print
    a = int(str(z)[0])
    print "First Number = " + str(a)
    print
    b = int(str(z)[1])
    c = int(str(z)[2])
    print "In between = " + str(b) + str(c)
    d = int(str(z)[-1]) # treat z as string, take first string after . from z and format it back to int
    print "Last Number = " + str(d)
    print
    print "Rearrange Numbers"
    res = str(a+d) +str(b) +str(c)
    to_int = int(res)
    dif = y - to_int
    add = to_int + dif
    print "number = " + str(add)

    Continue reading...

Compartilhe esta Página