简体   繁体   中英

How to print the string that caused the if statement to be true python3

Looking for a way to print the string that caused the if statement to be true: Any help would be great for example

if (a == b):
 print(string that caused a to be equal to b) 

Two solutions, depending on the context:

  • In general programming, you would do the "if" statement, then print out whatever you want when the condition is true:

     if a == b: print(f"The strings are equal, {a}=={b}")
  • When writing tests, checking that things are equal and printing out any differences is very common, so there are functions for that. The details depend on the testing framework you're using, but a common pattern would be something like:

     self.assertEqual(a, b, "Reason why the strings should be equal")
  • In both cases, you can also include any other relevant information in the text you're printing out; for instance, if a and b are calculated based on other information, perhaps in a loop, you can include that information in the string:

     if a == b: print(f"The strings are equal, {a}=={b}, with x: {x}, y: {y}, z: {z}")
     self.assertEqual(a, b, f"Should be equal for x: {x}, y: {y}, z: {z}")

If you're looking to print the conditional in the if statement that evaluated to True , you can do this by raising an empty error right after and checking out the stack trace:

In [1]: if 1 == 1:
   ...:     raise
   ...: 
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-7c0520e91bc3> in <module>
      1 if 1 == 1:
----> 2     raise
      3 

RuntimeError: No active exception to reraise

This only works because you get a small code-window of context in the stack trace, so you wouldn't see it if you didn't this.

In [3]: if 2 == 2:
   ...:     x = 1
   ...:     y = 2
   ...:     z = 3
   ...:     raise
   ...: 
   ...: 
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-bea1040009ca> in <module>
      3     y = 2
      4     z = 3
----> 5     raise
      6 
      7 

RuntimeError: No active exception to reraise

Notice you can't see the 2==2 in that output.

However, if you're asking about the memory of variables in the conditional:

In [5]: x = 1
   ...: y = 2
   ...: 
   ...: other_code = 1
   ...: other_code = 1
   ...: other_code = 1
   ...: 
   ...: if x + 1 == y:
   ...:     raise ValueError("Can I see the lines 'x = 1' and 'y = 2' here?")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-e9dd345d02db> in <module>
      7 
      8 if x + 1 == y:
----> 9     raise ValueError("Can I see the lines 'x = 1' and 'y = 2' here?")

ValueError: Can I see the lines 'x = 1' and 'y = 2' here?

I'm afraid I do not know that any such thing, other than a debugger, exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM