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
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
Several expressions are counted, so each one identifies 1,2, etc.
Quote:Several expressions are counted, so each one identifies 1,2, etc.
This question is 100% cryptic, but maybe it's just me...
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
(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
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}')
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.