views:

82

answers:

4

Hi guys, i have a function that give me the result that im expecting in console mode, but if i try to use the function with Django, the page never load and just have a loop calculating and never end.

Any idea ? *sorry with my english

Console function (WORK GREAT):

def sum_digitos(n):
    sum = 0;
    while n != 0:
        sum += n % 10
        n /= 10

        if sum > 9:
            x = str(sum)
            y =list(x)
            sum = int(y[0]) + int(y[1])

    return sum

print sum_digitos(2461978)

Django views:

def Calcular(request):
        if request.method == 'POST':
            form = NumerologiaForm(request.POST)
            if form.is_valid():
                sum = 0;

                ano = str(request.POST['fecha_year'])
                mes = str(request.POST['fecha_month'])
                dia = str(request.POST['fecha_day'])

                data = dia + mes + ano
                fecha = int(data)

                while fecha != 0:
                    f = fecha
                    sum += f % 10
                    f /= 10

                    if sum > 9:
                        x = str(sum)
                        y =list(x)
                        sum = int(y[0]) + int(y[1])

                resultado = get_object_or_404(Numero,numero = sum)
            return HttpResponseRedirect(resultado.get_absolute_url())
        else:
            form = NumerologiaForm()
        return render_to_response('numerologiaForm.html',{'form':form})
+4  A: 

Try:

        f = fecha
        while f!= 0:
            sum += f % 10
            f /= 10

            if sum > 9:
                x = str(sum)
                y =list(x)
                sum = int(y[0]) + int(y[1])

It seems you were changing f, but checking fecha for the looping.

Sanjay Manohar
Hence, an infinite loop because `fecha` is never `0`.
Mike DeSimone
A: 

You don't say what the rest of your environment is like, but you should be using f //= 10 to ensure that you're performing integer division.

Ignacio Vazquez-Abrams
+1  A: 

There's no need to go to all that work to sum the digits in that number, because the sum of the digits is num % 9. If num % 9 is zero, then the actual sum of digits is 9.

By changing your method to

def sum_digitos(n):
    sum_ = n % 9
    return sum_ if sum_ != 0 else 9

You will completely avoid whatever issue was happening inside your original method.

Mark Rushakoff
But that stops working once the digits add up to more than nine.
Adam
@Adam: No, it really doesn't.
Ignacio Vazquez-Abrams
My apologies... in my head I was thinking the question was for a single pass sum of the digits (I guess I skipped the whole "numerology part").
Adam
+2  A: 

Sanjay's answer is the correct one, and I recommend it. I just wanted to ask why you didn't just do:

from numerology import sum_digitos

def Calcular(request):
    # In your code, you return HttpResponseRedirect using a nonexistent 
    # "resultado" variable if the form is not valid. This will raise an 
    # exception. I think you meant to indent "return Http..." one step more.

    if request.method == 'POST':
        form = NumerologiaForm(request.POST)
    else:
        form = NumerologiaForm()

    # "or..." part of next line not needed if form.is_valid() returns 
    # False for a blank form.
    if not form.is_valid() or form == NumerologiaForm():
        return render_to_response('numerologiaForm.html', {'form': form})

    ano = str(request.POST['fecha_year'])
    mes = str(request.POST['fecha_month'])
    dia = str(request.POST['fecha_day'])
    resultado = get_object_or_404(Numero, 
        numero=sum_digitos(int(dia + mes + ano)))

    return HttpResponseRedirect(resultado.get_absolute_url())

You had a working function in Python already... why not just import it and use it?

Mike DeSimone
Mike, yeah, just importing the sum_digitos Function and is working :) thank guys :)
Asinox