Python Forum
Conditional evaluation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Conditional evaluation (/thread-33826.html)



Conditional evaluation - stsxbel - May-31-2021

Good day. Please tell me how to correctly compute with the condition? there is a code:
a1 = float(result1[0] * 0.1)
b1 = float(row[2])
c1 = b1 - a1
print (f'{c1:.lf}')
How to make a condition if c = negative value, then write to the database, if positive, then use the formula:
с1 = b1 - 6553.5 - a1
and write the result to the database


RE: Conditional evaluation - perfringo - May-31-2021

This is called conditional expression.

It would be good to be precise in your question. There is no c in your code. You have c1

c = c1 if c1 < 0 else b1 - 6553.5 - a1



RE: Conditional evaluation - stsxbel - May-31-2021

Several expressions are counted, so each one identifies 1,2, etc.


RE: Conditional evaluation - Gribouillis - May-31-2021

Quote:Several expressions are counted, so each one identifies 1,2, etc.
This question is 100% cryptic, but maybe it's just me...


RE: Conditional evaluation - stsxbel - Jun-03-2021

It does not work for me like this:
    a1 =  float(result1[0] * 0.1)
    b1 =  float(row[2]) #row2- 1продукт
    silos1a = b1 - a1
if silos1a < 0
    print ("Расход:"f'{silos1a:.1f}')
else b1 - 6553.5 - a1
    print ("Расход:"f'{silos1a:.1f}')
SyntaxError: invalid syntax


RE: Conditional evaluation - snippsat - Jun-03-2021

(Jun-03-2021, 03:58 AM)stsxbel Wrote: It does not work for me like this:
There are several error here,should look a some tutorials as these errors are really basic.
a1 =  float(0.1)
b1 =  float(3.4)
silos1a = b1 - a1
if silos1a < 0:
    print(f'Расход: {silos1a:.1f}')
elif b1 - 6553.5 - a1:
    # Should maybe do some stuff here
    print(f'Расход: {silos1a:.1f}')
Output:
Расход111: 3.3



RE: Conditional evaluation - stsxbel - Jun-13-2021

It worked in this version:
a1 =  float(140.2)
b1 =  float(6543.5)
silos1a = b1 - a1
if silos1a > 0:
    print(f'Расход: {silos1a:.1f}')

else:
    silos1a = b1 - 6553.5 - a1
    print(f'Расход: {silos1a:.1f}')



RE: Conditional evaluation - Gribouillis - Jun-13-2021

It seems that you inverted the condition implied by your first post! You could also try something like
>>> a1, b1 = 140.2, 6543.5
>>> silos1a = b1 - a1 - bool(b1 > a1) * 6553.5
>>> print(f'Расход: {silos1a:.1f}')
Расход: -150.2
This amounts to using bool() as a programmatic version of the Iverson bracket used by mathematicians.