Python Forum

Full Version: why doesn't python look in two directions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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")
(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 answer is 4: true is in Andromeda Galaxy but it looks in Magellanic Clouds
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'))
when the true is in two directions he looks for the shortest path but the true in bottom
The tests are checked in the order they appear and the first one that succeeds determines which branch is entered.
(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
OK? Maybe you should write the code in a different way to get the result you expect?
(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
Pretty much any language works like this and that isn't likely to change.
Pages: 1 2 3