Skip to main content
1-Visitor
May 25, 2020
Question

Declare a variable based on a conditional

  • May 25, 2020
  • 3 replies
  • 1690 views

I am trying to declare a variable based on a conditional, the following code is an idea that illustrates the intention:

 

import numpy as np

a = 23
b = 54
c = 60

 Qs = if a < b:
 Qs = 1
 elif a < b < c:
 Qs = 1.4*a
 else:
 Qs = 0.69*b

Best regards.

3 replies

23-Emerald IV
May 25, 2020

You're not declaring a varibale, but assigning it a value based upon a condition.

Two of many possible implementations:

LucMeekes_0-1590406935117.png

Success!
Luc

23-Emerald I
May 25, 2020

Or, courtesy of Luc Meeks

FredKohlhepp_1-1590425698591.png

 

23-Emerald IV
May 25, 2020

Fred,

 

I'm afraid this is not correct. There's an else-if between the first and second condition. In this case, with the given a, b and c, the first two conditions are both true,

LucMeekes_0-1590434428239.png

 

but the first must prevail, becasue of the else-if.

The 'return's in my program take care of this else-if. the if() function does it also, but the vector operations don't.

Maybe this mends it:

LucMeekes_1-1590434568406.png

With that:

LucMeekes_2-1590434663133.png

you get

LucMeekes_3-1590434682050.png

LucMeekes_4-1590434733992.png

and

LucMeekes_5-1590434748743.png

 

Luc

25-Diamond I
May 26, 2020

Are you sure about the condition a<b ?

Shoudn't it rather be something like b<=a ?