Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional evaluation
#1
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
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Several expressions are counted, so each one identifies 1,2, etc.
Reply
#4
Quote:Several expressions are counted, so each one identifies 1,2, etc.
This question is 100% cryptic, but maybe it's just me...
Reply
#5
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
Reply
#6
(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
Reply
#7
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}')
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,620 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Evaluation of two different list in python? go127a 8 3,547 Apr-22-2019, 12:49 PM
Last Post: snippsat
  list evaluation go127a 2 2,430 Apr-12-2019, 11:13 AM
Last Post: DeaD_EyE
  operand evaluation order? insearchofanswers87 2 2,790 Sep-26-2018, 07:51 PM
Last Post: insearchofanswers87
  Recursive evaluation in parsley parser mqnc 0 2,551 Apr-13-2018, 09:02 PM
Last Post: mqnc
  Learning Python, newbie question about strings and evaluation new_learner_999 13 5,857 Feb-18-2018, 03:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020