futurecoder
futurecoder copied to clipboard
Swap variables exercise
Maybe we should add an exercise at the end of the variables chapter where users have to swap variables in the classic way:
x = 'x'
y = 'y'
z = x
x = y
y = z
print(x)
print(y)
Getting this right would reinforce their understanding of variables. However it's quite tricky for a beginner, so I think we should make it easier for them. There's many possible ways to do this. One possibility is to give them a bunch of possible lines to choose from:
x = x
x = y
x = z
y = x
y = y
y = z
z = x
z = y
z = z
and they have to pick the ones to put in the middle to solve the problem. Chances are they will first try x = y; y = x and when that doesn't work they realise the z lines are actually there for a reason. Thoughts?
This is very tricky indeed.
The Pythonic way to do this is simultaneous assignment: x, y = y, x
I'm guessing that this exercise would be in the earlier sections that deal with variables. It would be difficult to justify this exercise to the user, especially since the much more intuitive looking, easier Pythonic solution is available.
Also it seems to me that even if the user guesses the correct solution from the choices, they might not know exactly why it's correct. This could still build intuition, but also cause confusion.
The advantage of Python is to sweep these "pointer arithmetic" type of low level matters under the rug and code at a higher level. So that beginners and non-programmers can get their work done without worrying about these things. This stuff gets even more seasoned programmers. Also this type of swapping is usually discouraged (but at some point it may be unavoidable I guess).
So if these matters are gonna be covered, they should be covered properly. I think there can be a page dedicated to explaining how naming works (I know there is already some discussion, with a link to the article). Discuss the dangers involved, then the exercise, then finally the safer, Pythonic way to do it.
Is Python Tutor available that early in the course? If they could see it visually, with names pointing to things with arrows, it might be easier to understand.
This is at the end of chapter 3 on variables. It's just after they've seen this program:
word = 'Hello'
name = 'World'
sentence = word + ' ' + name
print(sentence)
word = 'Goodbye'
print(sentence)
The idea is to make sure that they have a solid foundation of the basics of variables and understand why for example the above program doesn't 'work' or why x = y; y = x won't swap the variables. This is more basic than the stuff about aliases to mutable objects discussed later.
Maybe this is the wrong exercise for that purpose. Can you think of something else similar? The chapter is composed almost entirely of verbatim steps and that worries me.
I think "Storing Calculations in Variables" page is appropriate for its level. Do you think it's lacking something?
It's very difficult to come up with an exercise that's not artificial or contrived. I had a few ideas (a bank account balance update, and a rock-paper-scissors game) but they are all convoluted versions of your exercise.
If you really want an exercise step in there, maybe we can go ahead with your original idea. It would definitely require a message step for x = y, y = x though.
Let's explain object references properly, then add the swap exercise. They should be able to make the connection.
>>> first_num = 5
>>> first_num
5
>>> second_num = first_num
>>> second_num
5
>>> second_num = 10
>>> first_num
5
>>> first_num = "number"
>>> second_num
10
(now the reference to 5 is lost)
OK, I feel very unsure about this whole thing now, and it's not really worth it. I think let's put it aside and maybe come back to it later if we have new ideas or someone else chimes in.
OK, you're the boss.
What can I do next? Tic-tac-toe maybe?