1

I try to fill the fields with the condition if / else but it does not work. I do not know where is my mistake in the code.

def TextValue ( !Classe! ):
if !Classe! == "Steppe arbustive" 
  or !Classe! == "Steppe arbustive à arborée" 
  or !Classe! == "Steppe arborée" 
  or !Classe! == "Savane arbustive" 
  or !Classe! == "Savane arbustive à arborée" 
  or !Classe! == "Savane arborée à boisée" 
  or !Classe! == "Savane boisée" 
  or !Classe! == "foret galerie relictuelle" :
           return "Végétation naturelle"

elif !Classe! == "Culture pluviale / jachère" 
 or  !Classe! == "Culture maraichère" :
           return "Zone de culture"

elif !Classe! == "Cours d'eau"
 or !Classe!== "Mare" :
           return "Surface hydrique"



Codeblock
TextValue ( !Classe! )
4

The first line should not contain the field call (!Classe!). Your code should look like this...

def TextValue ( classe ):
  if ((classe == "Steppe arbustive") 
    or (classe == "Steppe arbustive à arborée") 
    or (classe == "Steppe arborée") 
    or (classe == "Savane arbustive") 
    or (classe == "Savane arbustive à arborée") 
    or (classe == "Savane arborée à boisée") 
    or (classe == "Savane boisée") 
    or (classe == "foret galerie relictuelle")):
           return "Végétation naturelle"

  elif ((classe == "Culture pluviale / jachère")
    or  (classe == "Culture maraichère")):
           return "Zone de culture"

  elif ((classe == "Cours d'eau")
    or (classe == "Mare")):
           return "Surface hydrique"

When you send the field call to the the code block you need to assign its value to a new variable in the pre-logic. In addition, you need to indent the first "if". Also, I added parentheses because I like them :)

  • Thanks for your reply i tried your code and it works very fine. thanks a lot, i really like your parentheses. It will be also interesting to try the mylist that @Branco told me – Benilde Aug 28 '15 at 15:09
  • The inner parentheses are unnecessary here. It is usually regarded as poor style to include unnecessary parentheses in Python, since they make the code look more cluttered and somewhat more difficult to read. (The outer ones are only necessary to simplify the line continuation, which is good style for a statement this long.) – jpmc26 Aug 28 '15 at 20:55
  • @jpmc26 I think I would like a reference for that to make it sound less subjective. Personally, I think the inner parentheses make it easier to read, but again, I'm willing to concede if you have a reason other than opinion. – GeoJohn Aug 28 '15 at 21:33
  • Google's style guide explicitly discourages it. Additionally, it's something I've picked up on as uncommon. Take a look at this example. Obviously, in cases where the operator precedence isn't obvious (mixed or/and), they add clarity. In this case, it's well known that or has a higher precedence than ==, so the inner parentheses don't add any clarity, especially since it's one condition per line. – jpmc26 Aug 28 '15 at 22:13
  • @jpmc26 Thank you for the reference. I've never read Google's style guide, only van Rossum's from python.org. – GeoJohn Aug 31 '15 at 13:47
3

Your code isn't formatted correctly and you're using the wrong things for parameters.

def TextValue ( input ): #needs to be a variable, not your field
    if input  == "Steppe arbustive" 
      or input  == "Steppe arbustive à arborée" 
      or input  == "Steppe arborée" 
      or input  == "Savane arbustive" 
      or input  == "Savane arbustive à arborée" 
      or input  == "Savane arborée à boisée" 
      or input  == "Savane boisée" 
      or input  == "foret galerie relictuelle" :
           return "Végétation naturelle"

    elif input  == "Culture pluviale / jachère" 
      or  input  == "Culture maraichère" :
           return "Zone de culture"

    elif input  == "Cours d'eau"
      or input == "Mare" :
           return "Surface hydrique"


Codeblock
TextValue ( !Classe! )

Your field value gets passed into the code block and not the actual function definition. Also, you might find it easier to just use a list and an in check instead.

For instance, declare your list:

myList = ["Steppe abrustive", "Steppe arbustrive à arborée"] #etc etc

Then in your check, instead of massive or combos, just use an in:

if input in myList:
  #do something

Also, another thing you might be able to do is to use the in just to check your individual string if there is something common in your field. The in operator lets you check if a string is within another string, too.

  • Thanks for reply. I tried the mylist but it doesn't maybe i did a mistake in the code – Benilde Aug 28 '15 at 15:04
  • Take your code and post it as another question. That way we can keep the concerns separated. Also, throw in a few sample values from the data just in case there is a casing or so mismatch. – Branco Aug 28 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.