cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Declare a variable based on a conditional

Jake5237
2-Explorer

Declare a variable based on a conditional

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.

4 REPLIES 4
LucMeekes
23-Emerald III
(To:Jake5237)

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

Or, courtesy of Luc Meeks

FredKohlhepp_1-1590425698591.png

 

LucMeekes
23-Emerald III
(To:Fred_Kohlhepp)

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

Werner_E
25-Diamond I
(To:Jake5237)

Are you sure about the condition a<b ?

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

Announcements

Top Tags