Python Forum

Full Version: Weird behaviour using if statement in python 3.10.8
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hello, can someone explain me this wired behavior? At the if statement which you can see in the screenshot, despite if 1==2 (for test only) is false, the statement gets executed which is shown by the yellow part (I have set a breakpoint in VS Code).

[Image: pwBH1Tg]
Post code, not broken links to images.
(Jan-15-2023, 03:01 PM)deanhystad Wrote: [ -> ]Post code, not broken links to images.
To be honest I don't know why there is no possibility like in other forums to copy and paste images. Nevertheless, the code is simple and that's why I don't understand the behavior:

# if bool(data):
if (1 == 2): # For test purpose only
  print('[...]')   # This code gets executed
You can paste images. I've seen it done. But for nearly every kind of discussion about programs and errors, text is superior to a screen shot. Here, for example. I ran your code and it does not print [...]. I am not running Python 3.10.8, but I ran it on 3.10.4 and 3.11, and there are no reported logic or comparison problems for 3.10.8.

Please post an example that demonstrates the behavior you are seeing.
1 does not equal 2 so will always be false
The brackets are not required in the if statement,
print(1 == 2)
if 1 == 2:
    print("[...]")
print("Ended")
Output:
False Ended

(Jan-15-2023, 05:03 PM)mikepy Wrote: [ -> ]To be honest I don't know why there is no possibility like in other forums to copy and paste images.
(Jan-15-2023, 05:16 PM)deanhystad Wrote: [ -> ]You can paste images.
But the forum rules advise people to not post images, so please don't.

https://python-forum.io/misc.php?action=help&hid=21 Wrote:What to NOT include in a post
Images of text. Please do not take a screenshot of your traceback or code, just copy and paste the text of it in the post using BBCode. If this text is within a popup that you cannot copy the text out of, please use this tool to acquire the text from within it (for windows users). We need to copy and paste your code and cannot do so with an image.
If we are going to critique code, there is no reason to do this either "if bool(data):". You will get the same results using "if data:".
(Jan-15-2023, 05:27 PM)Yoriz Wrote: [ -> ]1 does not equal 2 so will always be false
The brackets are not required in the if statement,
print(1 == 2)
if 1 == 2:
    print("[...]")
print("Ended")
Output:
False Ended

Yes, that's exactly my problem why when the expression is false the print statement gets executed.
As an observation, from the first post, maybe @mikepy ran this?

if (1 == 2) is False:
    print("This print function will execute")
... but it's only my best guess.
(Jan-15-2023, 05:27 PM)Yoriz Wrote: [ -> ]But the forum rules advise people to not post images, so please don't.
Ok I will keep this in mind.
(Jan-15-2023, 05:51 PM)rob101 Wrote: [ -> ]As an observation, from the first post, maybe @mikepy ran this?

if (1 == 2) is False:
    print("This print function will execute")
... but it's only my best guess.
I have also tried your code that it right. I had done this only for test purposes to see why this behavior occurs.
(Jan-15-2023, 05:16 PM)deanhystad Wrote: [ -> ]You can paste images. I've seen it done. But for nearly every kind of discussion about programs and errors, text is superior to a screen shot. Here, for example. I ran your code and it does not print [...]. I am not running Python 3.10.8, but I ran it on 3.10.4 and 3.11, and there are no reported logic or comparison problems for 3.10.8.

Please post an example that demonstrates the behavior you are seeing.
I have tried it before but the image was broken. So I have tried another online provider now to upload my screenshot. As you can see the code gets executed (yellow part)
[Image: test.png]
Pages: 1 2 3