简体   繁体   English

如何打印导致if语句为真的字符串python3

[英]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 语句为真的字符串:任何帮助都会很好,例如

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”语句,然后在条件为真时打印出您想要的任何内容:

     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:例如,如果ab是根据其他信息计算的,可能在循环中,您可以将该信息包含在字符串中:

     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:如果您希望在if语句中打印条件为True ,您可以通过在之后立即引发一个空错误并检查堆栈跟踪来做到这一点:

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.请注意,您在 output 中看不到2==2

However, if you're asking about the memory of variables in the conditional:但是,如果您询问条件中的变量memory

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.恐怕我不知道除了调试器之外还有任何这样的东西存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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