Problem with IF statements

26 Feb, 2015

Art S writes (excerpted):

First, let me say that I am having a great time learning Python from Python for Kids, even though I am just a kid at heart. I'm an old, old guy (I'll be 52 in April), but my students keep me young - I've been teaching high school mathematics for 25 years, and I am finally getting around to checking "Learn Python" off of my to-do list.

Here's the problem. All of my code has been running fine through IDLE until I got to the chapter on IF statements. When I typed into IDLE the first example from Chapter 5, page 54, nothing happened - the string 'You are too old!' (coincidence?) was not returned. All of the other examples involving IF's lead to the same result - nothing.

if-statement

I thought I would give it one more try, just to be sure. When I did, I accidentally hit enter twice, and the desired output appeared. I feel good that it worked, but why should I have to hit enter twice?

So there's nothing you're actually doing wrong here. When you hit enter after typing the second line (the print part), Python is just waiting for the next statement in that block of code. Python uses white-space for the program structure, so the only way it knows you've actually finished that particular block of code, is when you hit enter again on the next line. So Python only runs the code once you've actually said you've finished... and the way you say you're finished, in this case, is to hit enter on an empty line.

.....

Miquel C writes:

Do you know where to learn Python? I've been searching and I've only found Codecademy. I'd be very happy if you know where to go from Python For Kids. All kind of learning would be great.

I also downloaded an IRC bot, and I've translated it to my language. I've been looking the code and learning a bit.

Once you've mastered the basics of programming, my suggestion would be to find a small project to work on. Rather than looking at other people's code, or reading another, more advanced book, try creating your own version of an application you're interested in. It should be challenging, but not impossible. For example, when I was younger, I learned a new programming language by creating my own version of Tetris. You'll hit problems you don't know how to solve, so you'll end up reading the Python documentation, or googling to find the answers, or asking questions on sites like StackOverflow. But that's the one of the best ways to carry on learning (in my opinion).