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
#1
this stupid code is created on purpose, the interpreter reacts to the truth and executes it, but if the truth is in two directions, it chooses a short path despite the fact that somewhere there is a more precise argument

I write bad code on purpose, never do this !!!
''' if i enter 4 it shows Magellanic Clouds
but more accurate answer is Andromeda Galaxy
'''
''' answer 4 does not equal 5, the program can have truth in the short or long direction ''' 
q1 = int(input("What number?: \n"))
if q1 != 5 : # I specifically created conditions to see how the language reacts when two directions. i know programers not write like this. 
    # first  direction short way
    if q1==6:
        print("Canis Major Dwarf Galaxy")
    elif q1==7:
        print("Cygnus A")
    else :
        print("Magellanic Clouds") '''python's answer here he completely ignored other directions despite the fact that a more accurate answer == 4 in another direction'''              
# second direction long way
elif q1==4: # direct answer here in the condition I will write 4
    print("Andromeda Galaxy") 
else :
    print("Milky Way Galaxy")
Reply
#2
(Jan-01-2021, 06:25 PM)Kakha Wrote: smart python should show Andromeda Galaxy
why? Nope... It just does what you tell it.
smart programmer should not write "smart" if statements.
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
#3
if answer is 4: true is in Andromeda Galaxy but it looks in Magellanic Clouds
Reply
#4
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'))
Reply
#5
when the true is in two directions he looks for the shortest path but the true in bottom
Reply
#6
The tests are checked in the order they appear and the first one that succeeds determines which branch is entered.
Reply
#7
(Jan-01-2021, 06:48 PM)ndc85430 Wrote: The tests are checked in the order they appear and the first one that succeeds determines which branch is entered.

yes This is a problem, ignores the second branch completely
Reply
#8
OK? Maybe you should write the code in a different way to get the result you expect?
Reply
#9
(Jan-01-2021, 06:58 PM)ndc85430 Wrote: OK? Maybe you should write the code in a different way to get the result you expect?

i am a beginner. it will be super if python becomes smarter
Reply
#10
Pretty much any language works like this and that isn't likely to change.
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,345 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 13,303 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