And hello again.

I’ll start this off with an obvious truth. The location of if/elif statements is very important. Well of course they’re important, right?

Backstory: I was working on a problem from the MIT python course on a ‘rock, paper, scissors’ game. I could have gone the rout of simply naming each winning condition (i.e., if player1=rock and player2=paper or player1=paper and player2=rock or player1=scissors and player2=paper, etc. etc.), but I wanted to go with a formula instead to use a ‘>’ or ‘>’ to determine the winner.

So I set initial values for each player (value1 and value2), and set rock’s value at 3, scissors at 2 and paper at one. This way I could just say if value1>value2, print ‘player1 wins.’ I’d just need an else statement for if paper and rock were chosen (because paper beats rock, rock doesn’t beat paper, or rather, 1 is greater than 3 in that instance), and an else statement for if they tie.

Simple enough right? I hammered out my program and tested it. But it kept giving me the wrong winner for paper versus rock. I swapped my > and < signs around and messed with it for a while trying to figure out what was wrong.

As it turns out, the elif statement i had for rock and paper should have been at the TOP of the if-elif series. I had been calculating whether rock was greater than paper all along, which is always true, so my program never got to the elif player1 is rock and player2 is paper exception.

This was almost counterintuitive for me because when I program, I want to get the parts of the program written that do the most work (i.e, 3 is greater than 2 and 2 is greater than 1, so that would only leave one tiny else statement needed for 3 and 1). But in programming, this is backwards. You need those “small” exceptions up front, or your program will not work as you hope it will.

Yeah, it’s a pretty basic concept, but one I had to learn the hard way. Hopefully it’ll stick this time.

All told, I got a successful program (with comments!!!) out of it, but it took me a while longer than it should have. In short, here’s to learning.