The Age of Adaline

What would the following python code print out?

1
2
3
4
5
6
current_year = 2015   
andy_birth = 1984   
andy_minus_adaline = 4   
adaline = andy - andy_minus_adaline   
andy = current_year - andy_birth   
print adaline

None of the rest 1988 27 31

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

The code gives a Traceback:

NameError: name 'andy' is not defined

Why? Because line 4 is executed before line 5 and when that happens, the interpreter has no clue what the variable andy refers to.

There are two important things that are highlighted here, which I always mention while teaching newbies:

  1. Algorithms are inherently procedural. They are executed line by line
  2. The = in programming is more often the same as a binding or an assignment, which is not the same in the way it is used in math

Follow up teaser

Moderator note:

Yup, those 2 points are really important!

@Calvin Lin , I slightly edited your code. Please check if this is what you meant?

Agnishom Chattopadhyay - 5 years, 8 months ago

Log in to reply

Yup! I started off with Brian, and then when I was changing the title, I wanted to play off "The age of Adeline". Thanks for editing out the last instance of Brian :)

Calvin Lin Staff - 5 years, 8 months ago

I got this right, just because I thought this would be Python 3 already and the print would need parentheses.

Julian Valentine - 1 year, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...