Posts: 22
Threads: 6
Joined: May 2022
Jan-15-2023, 02:59 PM
(This post was last modified: Jan-15-2023, 05:54 PM by Yoriz.
Edit Reason: Title
)
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).
Posts: 6,802
Threads: 20
Joined: Feb 2020
Post code, not broken links to images.
Posts: 22
Threads: 6
Joined: May 2022
Jan-15-2023, 05:03 PM
(This post was last modified: Jan-15-2023, 05:03 PM by mikepy.)
(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
Posts: 6,802
Threads: 20
Joined: Feb 2020
Jan-15-2023, 05:16 PM
(This post was last modified: Jan-15-2023, 05:16 PM by deanhystad.)
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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
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.
Posts: 6,802
Threads: 20
Joined: Feb 2020
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:".
Posts: 22
Threads: 6
Joined: May 2022
(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.
Posts: 453
Threads: 16
Joined: Jun 2022
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
Posts: 22
Threads: 6
Joined: May 2022
Jan-15-2023, 05:51 PM
(This post was last modified: Jan-15-2023, 05:56 PM by Yoriz.
Edit Reason: Reduced size of quote to only include relevant part
)
(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.
Posts: 22
Threads: 6
Joined: May 2022
Jan-15-2023, 05:59 PM
(This post was last modified: Jan-15-2023, 06:04 PM by Yoriz.)
(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)
Yoriz write Jan-15-2023, 06:04 PM:Not following directions after repeatedly informed
|