Python elif syntax

Hey guys, I am looking on my code and I can not get around what is wrong with my syntax. I am doing this kind of syntax in elif statement for the first time so I do not precisely know what I can not do in this kind of statement. In code bellow I get syntax error of my first elif statement. Do you have an idea what could be wrong?

def optimal_shiping(weight, drone = drone_shipping(weight), ground = ground_shipping(weight), premium = premium_ground_shipping):

  if (drone < ground and drone < premium):

    print('Drone Shipping')

  return drone

  elif (ground < drone and ground < premium): 

    print('Ground Shipping')

  return ground

  else:

    print('Premium Ground Shipping')

  return premium

Thank you for answers.

Hey there @bit5955278864 :grinning:

It is difficult to tell because your code is not properly formatted, and indentation is a big part of Python.

To properly format your code press this button:

And then insert your code here:


I can’t say for sure, since I can’t see your indentation, but if I took a guess I would say your first return might be outside of the if block:

condition = 5

def function(condition):
  if (condition < 10):
    print("true")
  return True             
#   \
# Notice the indentation makes the return outside of the if
# This effectivly ends the if/elif/else statement, and the elif on
# the next line is out of place and by itself.
#    /
  elif (condition > 10):
    print("false")

This results in: SyntaxError: invalid syntax

Remember that in Python for a line of code to be part of a block, it must be indented according to all the other lines in that block:

if True:
  print("This is true")
  print("This line of code is within an if block")
print("Though this one is not due to indentation")
2 Likes
def optimal_shiping(weight, drone = drone_shipping(weight), ground = ground_shipping(weight), premium = premium_ground_shipping):
  if (drone < ground and drone < premium):
    print('Drone Shipping')
  return drone
  elif ground < drone and ground < premium: 
    print('Ground Shipping')
  return ground
  else:
    print('Premium Ground Shipping')
  return premium ```
1 Like

As I said above,

Take a look at your indentation on the return statements.

3 Likes

Yeah I got it right now. Intentation was the problem.
And one more question about syntax.

Can I use something like this:

if a < (b and c): 
   ....

Or do I need to use :

if a < b and a < c
  ....
1 Like

To the best of my knowledge you would have to use your second option.

if a < (b and c): 

This would evaluate b as either True or False based on its value. If it is more than 0 it is considered True. It would than do the same thing for c.

It would then check if b and c are both True, and see if a is less than True or False.
Here is an example:

a = 5
b = 5
c = 5

#      A is more than 0, and is therefor True
#     /    b is more than 0 and is therefor True
#    /    /     c is also more than 0 and is therefor True
#   /    /     / 
if a < (b and c):
#    \     \
#     \     b and c is True since a is True and b is True
#      \
#     since the less than operator works on int or float
#     the program will use the number version of boolean values
#     True = 1, False = 0
#     since both sides evaluate to True and are therefor = to 1
#     neither side is more than the other.
1 Like

Thank you very much. I get it all now.

Have a great day man.

1 Like