Could someone please help explain?


#1



https://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096


I have fixed my own problem but would really appreciate some insight to see if i am grasping the concept properly.


In my code i received a error that price was referenced before assignment when i used ==. But came out correct when only using the one =. why is this causing a error when == runs fine on the other lines? example below of the wrong answer.


def rental_car_cost(days):
        price == 40 * days:
        if days >= 7 :
             price -= 50
         elif days >= 3 :
             price -=20
         return price


#2

>>> def rental_car_cost(days):
        price == 40 * days:     <<<
        if days >= 7 :
             price -= 50
         elif days >= 3 :
             price -=20
         return price
        
SyntaxError: invalid syntax
>>>

Notice it didn't see any error before that invalid token? But if we inspect price it will be True, not the product of 40 and days

==

is a comparator that seeks identity.

=

is an assignment operator. Think of the direction being right to left,

a <<-- expression

price = 40 * days

#3

Thanks! I appreciate the clarification.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.