Python Forum
Weird behaviour using if statement in python 3.10.8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird behaviour using if statement in python 3.10.8
#1
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]
Yoriz write Jan-15-2023, 03:42 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Post code, not broken links to images.
ndc85430 likes this post
Reply
#3
(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
Reply
#4
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.
ndc85430 likes this post
Reply
#5
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.
ndc85430 likes this post
Reply
#6
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:".
Reply
#7
(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.
Reply
#8
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
(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.
Reply
#10
(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]
Yoriz write Jan-15-2023, 06:04 PM:
Not following directions after repeatedly informed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 894 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 725 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,258 Apr-09-2023, 01:48 AM
Last Post: vugz
  Generator behaviour bla123bla 2 1,108 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,557 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,692 May-27-2021, 02:01 AM
Last Post: MDRI
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,723 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,834 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,505 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 2,434 Aug-18-2020, 07:51 PM
Last Post: JohnB

Forum Jump:

User Panel Messages

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