-5

I'm currently going through Codecademy's Python course, and am currently learning functions. In one of the snippets of code they give you, it says:

def tax(bill):
    """Adds 8% tax to a restaurant bill."""
    bill *= 1.08
    print "With tax: %f" % bill
    return bill

def tip(bill):
    """Adds 15% tip to a restaurant bill."""
    bill *= 1.15
    print "With tip: %f" % bill
    return bill

meal_cost = 100
meal_with_tax = tax(meal_cost)
meal_with_tip = tip(meal_with_tax)

Everything makes complete sense, except for bill *= 1.15 and bill *= 1.08. I know why they are what they are, however, I don't understand why they need an asterisk in front of the =. If I were to have written this, I would have just put bill = 1.15 and bill = 1.08. Why would that be wrong?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Connor Nichols
  • 11
  • 1
  • 1
  • 3
  • 1
    It's a shorthand for `bill = bill * 1.15`, so the value of the variable `bill` is multiplied by 1.15 and stored in `bill` again. – jojonas Sep 29 '15 at 17:25
  • 6
    Have you tried replacing `*=` with `=` and running the program? That may give a clue as to why you would use one over the other. – Kevin Sep 29 '15 at 17:25
  • @ Kevin, good tip, don't know why I didn't think of that. Should have done that first before I posted. Thanks. – Connor Nichols Sep 29 '15 at 17:30

1 Answers1

2

That's the same as:

bill = bill * 1.15

You could also do per example:

bill += 1.15

Which is the same as:

bill = bill + 1.15
jramirez
  • 8,537
  • 7
  • 33
  • 46