# Simple Question on IF and ElIF with inequalities

Doing a project in code academy related to package weights. The below code should output 35 for a package that weights 5, instead it prints 43.75

The code seems to work correctly for a package that weights 8.4 , it prints 53.6.

I can’t figure out what’s happening. TIA!

def ground_shipping(weight):
if weight <= 2.0 and:
return weight * 1.50 +20
elif weight < 2.0 and weight <= 6:
return weight * 3 + 20
elif weight > 6 and weight <= 10:
return weight * 4 + 20
else:
return weight * 4.75 +20

#Check Ground Shipping
print(ground_shipping(8.4))
print(ground_shipping(5))

1 Like

Seems to be something missing after the first `and` following `if weight<=2.0`. Also, the first `elif` condition doesn’t seem right. Every number less than 2 is also less than 6. Hope this helps.

P.S. These are back tics.

```
Paste code here
```
2 Likes

Hi sorry, thought I used the code formatting. Note the “and” has been deleted, that was erroneous and was not causing the error.

``````def ground_shipping(weight):
if weight <= 2.0:
return weight * 1.50 +20
elif weight < 2.0 and weight <= 6:
return weight * 3 + 20
elif weight > 6 and weight <= 10:
return weight * 4 + 20
else:
return weight * 4.75 +20

#Check Ground Shipping
print(ground_shipping(8.4))
print(ground_shipping(5))
``````
2 Likes

But anyway you were right. The ELIF weight < 2.0 and weight >= 6: was incorrect. Thank you!

3 Likes

Python understands ternary statements such as
`lo_val < var < hi_val`
… which are, in my opinion, more easily read than `var < hi_val and var > lo_val`

Also, if you keep all of the carats pointing left, the statement is roughly analogous to a number line, further contributing to readability.

6 Likes

Thanks!

I believe I tried writing

`var > low_val and < high_val`

but that didn’t seem to work.

Mixing Boolean operators into different functions seem to produce strange outcomes.

for instance adding “And” to the below count function returns 5 for count “Jake and Cassie”

but only 9 when i add “Or” when i would expect 14

``````votes = ['Jake', 'Jake', 'Laurie', 'Laurie', 'Laurie', 'Jake', 'Jake', 'Jake', 'Laurie', 'Cassie', 'Cassie', 'Jake', 'Jake', 'Cassie', 'Laurie', 'Cassie', 'Jake', 'Jake', 'Cassie', 'Laurie']

#Count with boolean operators

``````

Question: What does the expression (‘Jake’ or ‘Cassie’) return?

``````print('Jake' or 'Cassie')

# Output:
'Jake'
``````

the expressions `x or y` and `x and y` return, not necessarily True or False, but rather one of the values x, y:

• The expression `x or y` returns the first (left to right) of the operands x, y which has a truth value of True. If neither is True, it returns y

• The expression `x and y` returns the first (left to right) of the operands x, y which has a truth value of False. If neither is False, it returns y

So if the operands x and y are expressions that evaluate to True or False (or themselves are the values True or False), the expressions behave as you’d expect.

But if they are any other type of object, they are assigned a truth value, based on the following rule:

• 0 (zero), False, None, any empty container (string, list, tuple, dictionary), or any object that evaluates to one of these have a truth value of False.

• Everything else has a truth value of True.

‘Jake’ and ‘Cassie’ are both non-empty strings, so `('Jake' or 'Cassie')` always returns ‘Jake’ and `('Jake' and 'Cassie')` always returns ‘Cassie’, and

`jake_or_cassie_votes = votes.count('Jake' or 'Cassie')` will always count the number of 'Jake’s
`jake_and_cassie_votes = votes.count('Jake' and 'Cassie')` will always count the number of 'Cassie’s

3 Likes

Wow! Ok. I think I get it . Weird way of thinking about things. Thanks!

1 Like