samedi 1 mai 2021

if else one liner in python

a=0
b=0
s="1110000"
for i in range(len(s)):
   if s[i]=='1':
      a+=1
   else:
      b+=1

this if else I need to write in one line. I tried below way it is not working.

a+=1 if if s[i]=='1' else b+=1

PLease help me on this

Create multiple strings based on elseif condition in php

While this code seems to work to change $tdstyle as anticipated on the td line, it does not update $font and $text appropriately based on the esleif conditions. It always seems to keep these strings the same based on the else condition alone even if it isn't true. Maybe just a syntax issue? I am certain there is a much better way to do this, but my limited knowledge has brought me to this point. Any suggestions?

<?php       
$num = (float)$uvindex;
            if($num >10) {
                $tdstyle='#B567A4';
                $font='white';
                $text='Extreme';
                } elseif($num >=8) {
                $tdstyle='#E53210';
                $font='white';
                $text='Very High';
                } elseif($num >=6) {
                $tdstyle='#F18B00';
                $font='black';
                $text='High';
                } elseif($num >=3) {
                $tdstyle='#FFF300';
                $font='black';
                $text='Moderate';
                } else $tdstyle='#3EA72D'; $font='black'; $text='Low';
            ?>
             <td height="82" colspan="2" align="center" valign="middle" class="data1" style="text-align:center; background-color:<?php echo $tdstyle ?>; color:<?php echo $font ?>; border: 3px solid black; border-radius: 7px; font-size:12px;">

In Python, does "if" evaluates the conditional statements in boolean context? [duplicate]

I am new to programming so apologies for not being able to frame the question properly but here's a more detailed version of the problem. If I run the following code:

age=2
if age:
   print("hello")
else:
   print("bye")

Output is "hello" which I read was due to age being evaluated to True.

But when I modify it to:

age=2
if age is True:
   print("hello")
else:
   print("bye")

The Output is "bye"

I was expecting the Output here to be True. Where is the gap in my understanding? I Would also be grateful if you could direct me to a link to study the topic. Thanks

Django Template - If both IF statements are empty

I know how to use an if statement in django, and if the if is empty. Like this:


<!-- False -->

Now how do I do this if I have more than on if statement like this:




<!-- Problem: -->

<!-- Do only if both are false -->

So I tried using if and but it didn't work, even if one existed it still ran the stuff in the else.

So how do I make sure it checks if both statements are false, and only if they are both false, to run the else. Any suggestions?

how to update global variable in if else statement (C++)

I am a newbie in C++ and it is a very easy task but all tutorials that I looked use cout some string in if-else conditions.

I want to update a global variable in if-else statement.

Here is the code;

        double theta_radian{};
        if (l2 >= l1)
        {
            double theta_radian = atan2(e2.y(), e2.x());
            std::cout << "theta_local" << theta_radian << std::endl;
            
        }
        else
        {
            double theta_radian = atan2(e1.y(), e1.x());
            
        }
        std::cout << "theta_radian" << theta_radian << std::endl;

I want to update theta_radian variable based on the if-else condition.

I don't want to create a vector and push_back to it since it is a simple practice and I want to learn how to do it properly.

Thanks in advance!

Why does my program keep skipping out an if statement?

My program is designed to change gears of a bike using DC motors with encoders. I have a few if statements so far basically saying if a button is pressed then run the motor until the position is met then turn off the motor. For example this first statement would be moving from gear 1 to gear 2. Then I have another statement saying if the button is pressed again then the motor will move to the next position. But when I run my program it skips the first statement because it doesn't stop at the position in the first if statement and keeps running until it gets to the position of the next if statement. I can't figure out why it does this. This is the code:

#define ENCA 2 // PINK
#define ENCB 3 // YELLOW

// this constant won't change:
//const int buttonPin2 = 3;
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
//const int ledPin = 13;       // the pin that the LED is attached to

//Motor A

int PWM = 5;
int in2 = 6;
int in1 = 7;

// Variables will change:
int pos = 0;
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
//int buttonState2 = 0;
int lastButtonState = 0;     // previous state of the button
//int lastButtonState2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ENCA, INPUT);
  pinMode(ENCB, INPUT);
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
  Serial.println("target pos");
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  //  pinMode(buttonPin2, INPUT);


  // initialize motor pins
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}


void loop() {

  if (buttonPushCounter == 1) {
  GearOneTwo();
  }
  if (buttonPushCounter == 2) {
  GearTwoThree();
  }
  PrintData();

}
void GearOneTwo() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
     }
  }
  if (pos >= 807) {
  MotorOff();
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void GearTwoThree() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
    }
  }
  if (pos >= 2000) {
  MotorOff();
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void PrintData() {
    int target = 888;
    Serial.print(target);
    Serial.print(" ");
    Serial.print(pos);
    Serial.println();
}

void MotorOff() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
}

void readEncoder() {
    int b = digitalRead(ENCB);
    if (b > 0) {
        pos++;
        }
    else {
        pos--;
    }
}

vendredi 30 avril 2021

How to check all input has value and do something

i have these inputs and i wanted to check every one of them has value then do something;

const tch_family_name = document.getElementById('tch_family_name');
const tch_lastname = document.getElementById('tch_lastname');
const tch_name = document.getElementById('tch_name');
const tch_phone = document.getElementById('tch_phone');
const first_alph = document.getElementById('first_alph');
const second_alph = document.getElementById('second_alph');
const third_alph = document.getElementById('third_alph');
const tch_bday = document.getElementById('tch_bday');
const textarea1 = document.getElementById('textarea1');

and I'm checking they have value or not like this

 function checkEmpty(check) {
        for (i = 0; i < check.length; i++) {
            if (check[i].value == "" || check[i].value == " " || check[i].value == null) {
                check[i].classList.add('inputErrorBorder')
            } else {
                check[i].classList.remove('inputErrorBorder')
            }
        }
    }

//mainInfo button id

  mainInfo.addEventListener('click', () => {
            test = [tch_family_name, tch_lastname, tch_name, tch_phone, first_alph, second_alph, third_alph, tch_bday, textarea1]
            checkEmpty(test) 
})

now how to do something when all input have value;

I tried else if() but it gave an incorrect result for example when first input don't value then it wont add inputErrorBorder class to a second or third inputs.

Please help;