Conditional statements


Good! Now, what happens when we want to combine and, or, and not together?

Imagine a car rental company in Spain: it will rent a car to any driver from Spain or to any foreign driver who is at least 26 years old:

driver_nationality = input('What is your nationality? ')
driver_age = int(input('What is your age? '))
if (driver_nationality == 'Spanish' 
  or driver_nationality != 'Spanish' and driver_age >= 26):
  print('You are good to go!')
  print('Sorry, no car for you!')

Which operator – or or and – has higher priority? Is this condition really:

(driver_nationality == 'Spanish' 
  or driver_nationality != 'Spanish') and driver_age >= 26

Or is it:

driver_nationality == 'Spanish' 
  or (driver_nationality != 'Spanish' and driver_age >= 26)

Python checks all conditions in the following order: not, and, or.

This means that the statement will be interpreted as:

driver_nationality == 'Spanish' 
  or (driver_nationality != 'Spanish' and driver_age >= 26)

If you want your conditions to be evaluated in another way, simply use parentheses. Actually, it's a best practice to always use them, as they improve the readability of your code.


In our imaginary online store, you can return an item and get a refund in one of two cases:

  • Within 14 days of the day of purchase, given that you have not used the item at all.
  • When the item broke down through no fault of your own, no matter when you bought it.

Write a program that will inform a user whether they can get a refund. Based on the answers, print either Refund or No refund.

Stuck? Here's a hint!

In your if statement, use both and and or. Introduce parenthes es to clearly state the order of operators.