Python Forum
Mysterious Indentation Problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Mysterious Indentation Problem (/thread-19264.html)

Pages: 1 2


Mysterious Indentation Problem - Dakodak - Jun-20-2019

Hello !

Spyder tells me i've an indentation problem here, line 4, "unexpected indent" :

def A(i, j, Nz, Nr, dr, dz, dt, he, Bir, Biz) :
    if i == 1 :
        if j == 1 :                                                              "partie 1"
            return ((1/(16*dt))+(1/(8*dz**2))+(1/(4*he*he*dr*dr))+(Biz/(8*dz)))
        elif j==Nr :                                                            "partie 3"
            return ((Nr-1)/(2*dt))+(Bir*(Nr-1)/(2*he*he*dr))+(Biz*(Nr-1)/dz)+((Nr-1)/dz**2)+((Nr-1.5)/(2*he*he*dr*dr))
        else :                                                                  "partie 2"
            return (j-1)*((1/(2*dt))+(1/(he*dr)**2)+(1/dz**2)+(Biz/dz))
    elif j==1 :
        if i==Nz :                                                              "partie 7"
            return (1/(16*dt))+(1/(16*dz**2))+(1/(4*dr*dr*he*he))+(Biz/(8*dz))
        else :                                                                  "partie 4"
            return (1/(8*dt))+(2/(8*dz))+(1/(2*he*he*dr*dr))
    elif i==Nz :
        if j==Nr :                                                              "partie 9"
            return ((Nr-1.5)/(he*he*dr*dr))+(Nr-1)*((1/2*dt)+(Bir/(2*he*he*dr))+(Biz/dz)+((1/dz**2)))
        else :                                                                  "partie 8"
            return (j-1)*((1/(2*dt))+(1/(he*he*dr*dr))+(1/(dz**2))+(Biz/dz))
    elif j==Nr :                                                                "partie 6"
        return ((Nr-1.5)/(he*he*dr*dr))+(Nr-1)*((1/(2*dt))+(Bir/(he*he*dr))+(Biz/dz)+(1/dz**2))
    else :                                                                      "partie 5"
        return (j-1)*((1/dt) + (2/(he*dr)**2) + (2/dz**2))
I've tried online website that shows errors in indentation, trying to replace tabs by spaces, avoiding blank lines, etc... Python doesn't want a loop in another loop (i have the same problem with a "while" loop in a "for" loop). It wants me to put the "return" line 4 at the same level as the "if" line 3. If I do it (because I've tried almost everything), the unexpected indentation error goes to the next line, etc. I don't know what to do, I've asked friends and read others forums (and this one too) before but I can't find a solution.

While you're here, it also does me a "syntax error" here, line 5 :

import numpy as np

""" Paramètres : """

tf = 5                                                                         "temps final"
N =  1                                                                         "nombre d'incréments de temps"
t_laser = 1                                                                    "durée de l'émission laser"
dt = tf / N                                                                    "pas de temps"
DT = dt * alpha / H^2                                                          "pas de temps adimensionné"
Thank you for your help !


RE: Mysterious Indentation Problem - ichabod801 - Jun-20-2019

It's because you put a string on the same line as the if, it sees that as a valid if in one line. Therefore the indentation is not valid. You seem to be using double quotes as comments, you should use an octothorpe (#) instead. That would also fix the other error.


RE: Mysterious Indentation Problem - Dakodak - Jun-21-2019

Omg it worked, thank you ! When i learned python, it was ok to put comments with double quotes so I didn't think it could be that :O


RE: Mysterious Indentation Problem - noisefloor - Jun-21-2019

Hi,

Quote:When i learned python, it was ok to put comments with double quotes so I didn't think it could be that :O
Never ever, it's alway wrong. Double quotes are a simple strings, nothing else. _ALWAYS_ use the # for comments and nothing else. That's the valid character for marking comments.

Regards, noisefloor


RE: Mysterious Indentation Problem - Dakodak - Jun-21-2019

Actually, it works but if they are "alone" on their line and not after something (like my "if" statements), I think I forgot that, my python lessons are a little bit far away in time ahah


RE: Mysterious Indentation Problem - DeaD_EyE - Jun-21-2019

The evaluations after return, should shifted in own functions.
Then you can use the functions instead. This will cleanup
the code a little bit. Give them verbs as names.

return ((1/(16*dt))+(1/(8*dz**2))+(1/(4*he*he*dr*dr))+(Biz/(8*dz)))
return ((Nr-1)/(2*dt))+(Bir*(Nr-1)/(2*he*he*dr))+(Biz*(Nr-1)/dz)+((Nr-1)/dz**2)+((Nr-1.5)/(2*he*he*dr*dr))
return (j-1)*((1/(2*dt))+(1/(he*dr)**2)+(1/dz**2)+(Biz/dz))
return (1/(16*dt))+(1/(16*dz**2))+(1/(4*dr*dr*he*he))+(Biz/(8*dz))
return (1/(8*dt))+(2/(8*dz))+(1/(2*he*he*dr*dr))
return ((Nr-1.5)/(he*he*dr*dr))+(Nr-1)*((1/2*dt)+(Bir/(2*he*he*dr))+(Biz/dz)+((1/dz**2)))
return (j-1)*((1/(2*dt))+(1/(he*he*dr*dr))+(1/(dz**2))+(Biz/dz))
return ((Nr-1.5)/(he*he*dr*dr))+(Nr-1)*((1/(2*dt))+(Bir/(he*he*dr))+(Biz/dz)+(1/dz**2))
return (j-1)*((1/dt) + (2/(he*dr)**2) + (2/dz**2))
Instead you'll do this later:
return function_1(dt, dz, he, Biz)



RE: Mysterious Indentation Problem - Dakodak - Jun-21-2019

Thank you but actually, I need this to be like I did ! In ordre to correct the expressions, I put comments to show which evaluation corresponds to which "region" of my problem. I've 15 or 20 functions like this, so another function for every evaluation wouldn't be readable, but I appreciate the help !


RE: Mysterious Indentation Problem - noisefloor - Jun-21-2019

Hi,

Quote:Actually, it works but if they are "alone" on their line and not after something (like my "if" statements)
Actually it works because technically you define a string without binding it to a name. Whoever told you that this is a comment, it's not. It's super mega ultra wrong. The only way to comment in Python is the #. Nothing else.

Regards, noisefloor


RE: Mysterious Indentation Problem - ichabod801 - Jun-21-2019

(Jun-21-2019, 11:11 AM)noisefloor Wrote: The only way to comment in Python is the #. Nothing else.

PEP 257 Wrote:String literals occurring elsewhere in Python code may also act as documentation.

I don't think it's quite as clear cut as "nothing else." I think of docstring just as a special kind of comment, and those require triple quotes.


RE: Mysterious Indentation Problem - noisefloor - Jun-21-2019

Hi,

Quote: I think of docstring just as a special kind of comment, and those require triple quotes.
I waited for that coming up... Docstrings are for documentation purposes, as the name says. Docstring can be read without looking into the source code, comments not. Comments give _additional_ info when reading through the source code, docstrings give info on how to use a class / function / ... - that's not the same.
And abusing docstrings for comments is still wrong.

Except this, the original post talked about double quotes, not triple quotes.

Regards, noisefloor