I want to write multiple if elif conditional which check if the value is greater than some number or less than some number or between two numbers

This is the example python code. I want to write that into Glide column. For example, there is a temperature column that user input. Then I want to use that column to create new column with the below conditions.

#Temperature
def news_temperature(score):
    if score <= 35.0:
        new_score = 3
    elif (score >= 35.1 and score <= 36.0) or (score >= 38.1 and score <=39.0):
        new_score = 1
    elif score >= 36.1 and score <= 38.0:
        new_score = 0
    elif score >= 39.1:
        new_score = 2
    return new_score

Try it with Java script and the Java script column.

Replace score with p1

4 Likes

The following in an if-then-else column should work:

  • If Score >= 39.1, then 2
  • If Score >= 38.1, then 1
  • If Score >= 36.1, then 0
  • If Score >= 35.1, then 1
  • Else 3

3 Likes

I have tried that but still cannot get the answer.

This is my javascript code

var newsScore;

if (p1 <= 35.0) {
    newsScore = 3;
} else if ((p1 >= 35.1 && p1 <= 36.0) || (p1 >= 38.1 && p1 <= 39.0)) {
    newsScore = 1;
} else if (p1 >= 36.1 && p1 <= 38.0) {
    newsScore = 0;
} else if (p1 >= 39.1) {
    newsScore = 2;
}

newsScore;

I didn’t get the return value in my new column.

Your example does work. I just need to put additional condition for when the input value is blank. Thank you very much for your help. So what I would like to understand is that we should write the conditionals starting from the biggest value?

Also may I know if we want to do some kind of machine learning model prediction, can we do that. For example, the input features will be the first 6-8 column features, then we will do some kind of model prediction on the last column. Can we do that?

Either way would work. If you did it from lowest to highest, then you would just reverse the comparison operators.

I’m not too sure about that, can you give a practical example with an expected result?

1 Like

image

For example, this kind of case. There will be multiple column structured data, then we will do some kind of model prediction. I saw some tutorial videos of building a chatbot app with OpenAI api but I am not sure if we have multiple input features, can we do model prediction? I haven’t tried that. Thank you very much again for your answer and prompt reply.

I’d say it’s possible, but you’d need to design some rules and craft an appropriate prompt for your AI.

The Glide Blog has quite a few recent articles and tutorials related to using AI in GlideApps. I’d suggest having a browse through there, and you might get some ideas.

1 Like

Thank you. I will try that.

Hola!

Just to tell you why your original code failed: the missing piece is the return() function.

return newsScore

This new version should work just as well as Darren’s suggestion.

 if (p1 <= 35) 
    return  3;
if ((p1 > 35 && p1 <= 36) || (p1 > 38 && p1 <= 39))  
    return  1;
if (p1 > 36 && p1 <= 38)  
    return  0;
  
return 2   // returns '2' if all checks fail (>39)!!.

Saludos!

4 Likes