0

I am trying to print the conditions that I have given in text format instead of printing the final value of conditions. Below is the sample code for better explanation.

a = 5
b = 10
condition = a>b and a+b<10
if condition:
    print"successful"
else:
    print"unsuccessful"
print("The conditions applied was",condition)

Here I want the system to print "a>b and a+b<10" but it prints "False" because the final value of condition is False

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • simpliest way would be to create a mapping between the condition and the value, e.g. `{'a>b and a+b<10': a>b and a+b<10}` – Chris_Rands Jul 16 '19 at 08:12
  • it's possible to `eval()` the string (but is unsafe if you're user accepting input) or build a proper parser – Chris_Rands Jul 16 '19 at 08:14
  • I have to use the condition at multiple places and I don't want to write the whole condition again and again at different if-else statements, so I save the expression in a variable "condition" to use the term "condition" directly whereever I need it and since with every condition I get different results, I want to save the results/output with the conditions ( "a>b and a+b<10") so that I can see later whcih conditions gave what results. So in the above example how shall I do this? Is there anyway to print it later and use a simple short variable instead of writing the whole condition again – Injeel Ahmed Jul 16 '19 at 08:19
  • It depends on your use case and Python level, but I believe this is the most elegant way I've seen https://stackoverflow.com/a/9558001/6260170 – Chris_Rands Jul 16 '19 at 08:21
  • @Chris_Rands See updated answer – IMCoins Jul 16 '19 at 08:25

2 Answers2

1

you can, but I do NOT recommend it, to store the condition in string form (for the printing part), then use eval when actually need the value. like this:

a = 5
b = 10
condition = 'a>b and a+b<10'
if eval(condition):
    print("successful")
else:
    print("unsuccessful")
print("The conditions applied was",condition)

Output:

unsuccessful
The conditions applied was a>b and a+b<10

AGAIN: USING EVAL IS A BAD AND DANGEROUS PROGRAMMING PRACTICE (some explanation why can be found here, and in many other places around the web)

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • 2
    Why are you writing an answer advising them what *not* too do? – Chris_Rands Jul 16 '19 at 08:17
  • 1
    Incidentally it has become very fashionable on SO to call 'eval()' bad under all circumstances, but it does have legitimate use cases – Chris_Rands Jul 16 '19 at 08:18
  • @Chris_Rands well, because in a simple enough script where you control the conditions it might be the simplest solution, avoiding the duplication of a safer solution... if that's what OP needs, he can choose to use it, but I won't be comfortable just leaving the answer without any disclaimer – Adam.Er8 Jul 16 '19 at 08:19
  • 1
    Thanks a lot guys, it worked for me perfectly. I am not taking any user input btw. Thanks both of you for quick response. – Injeel Ahmed Jul 16 '19 at 08:30
  • when doing data science work there is very little danger in using eval. – salamander Jan 13 '23 at 08:42
0

Since you updated your question in the comments, saying

I want to save the results/output with the conditions ( "a>b and a+b<10") so that I can see later whcih conditions gave what results.

I think you should use a lambda and the inspect module.

a = 5
b = 10
condition = lambda x, y: (x+y) > 10

if condition(a, b):
    print('condition passed !')
else:
    print('condition failed !')

And if you want to print the condition, you could see it doing...

from inspect import getsourcelines

print("The condition was...")
print(getsourcelines(condition)[0])
IMCoins
  • 3,149
  • 1
  • 10
  • 25
  • Thanks IMCoins, this worked as well. I got the solution. – Injeel Ahmed Jul 16 '19 at 08:30
  • A reasonable idea, although not full proof (e.g. fails in the Python interpreter). Also no reason to use `lambda` here, a regular `def` function is preferable when assigning the function to a name – Chris_Rands Jul 16 '19 at 08:31