Python Forum
why doesn't python look in two directions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why doesn't python look in two directions
#11
(Jan-01-2021, 07:03 PM)ndc85430 Wrote: Pretty much any language works like this and that isn't likely to change.

I really like this language. making mazes from if, very interesting. but short cuts are sometimes wrong
Reply
#12
(Jan-01-2021, 06:39 PM)deanhystad Wrote: I would like to hear what you think this code is supposed to do.

if statements are evaluated in the order they are written. This allows writing things like this;
if x < 5:
    do_a()
elfif x < 10:
    do_b()
else:
    do_c()
This would not work if Python could execute the statements in any order because 3 is < 5 and < 10 but you would only want to execue do_a().

For code such as yours I would use a dictionary instead of a bunch of if/ifelse statements.
bodies = {
    4: "Andromeda Galaxy",
    5: "Milky Way Galaxy",
    6: "Canis Major Dwarf Galaxy",
    7: "Cygnus A",
    8: "Magellanic Clouds"}

for key, value in bodies.items():
    print(f'{key} : {value}')
keys = bodies.keys()
q1 = input(f'Select ({min(keys)}:{max(keys)}): ')
print(bodies.get(int(q1), 'Invalid input'))
Thank you
Reply
#13
(Jan-01-2021, 07:11 PM)Kakha Wrote: but short cuts are sometimes wrong

Those are the rules of how it works and you just have to get used to them. It isn't that complicated, really.
Reply
#14
(Jan-01-2021, 06:38 PM)Kakha Wrote: if answer is 4
before that answer is different from 5 and is not 6 or 7, so Magellanic Clouds...
maybe you meant answer >=5 ?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
(Jan-01-2021, 07:20 PM)ndc85430 Wrote:
(Jan-01-2021, 07:11 PM)Kakha Wrote: but short cuts are sometimes wrong

Those are the rules of how it works and you just have to get used to them. It isn't that complicated, really.

The fact is that I was interested to know the principle. there is a short True and there is a long True. Python works with short True. when he sees ! = 5 before Else, it should check == 4 that's all
Reply
#16
I don't know what you mean by "short" and "long" true. In any case, the interpreter evaluates the code you give it, so since you wrote q1 != 5 and that was satisfied, that branch was executed. I don't know why you'd expect it to work any other way.
buran likes this post
Reply
#17
(Jan-01-2021, 07:34 PM)Kakha Wrote: it should check == 4 that's all
No, it should not. elif/esle means that no other condition was True before that branch. And in your case the very first branch/condition is True. As simple as that.
Maybe you are confusing if/elif/else construct with switch/case in some other languages where each branch/condition is checked. As mentioned, in python we use dict to implement that functionality.
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#18
(Jan-01-2021, 07:38 PM)ndc85430 Wrote: I don't know what you mean by "short" and "long" true. In any case, the interpreter evaluates the code you give it, so since you wrote q1 != 5 and that was satisfied, that branch was executed. I don't know why you'd expect it to work any other way.

For example, the police arrest a man named "123". There are two of them, and one lives near the police (short truth) and the other lives far away (long truth). who should be arrested?
Reply
#19
(Jan-01-2021, 07:51 PM)Kakha Wrote: who should be arrested?
depends what's written in the warrent (i.e. your code)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
I have no idea where you're going with that. I'd hope the police make arrests based on evidence.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,362 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 13,364 Sep-16-2019, 03:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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