This past week I've been going back and redoing some starting lessons over Python 3. Mostly so I can make sure I'm not just jumping through hoops, but also because each time I go through it I realize I'm learning something I missed the first time. This past week was no exception.
I'm auditing a class from Rice University on Coursera.com. The course is Interactive Programming in Python. This past week was to create a basic game of Rock-Paper-Scissors-Lizard-Spock in Python. The mini-lab walked you through everything, and even gave you a template to test your code one step at a time. I thought this was a very good idea. You can make a little bit of code, and test it immediately.
First they have you create two functions. The first takes the name called, such as "paper", and convert it to a number. And the second was to turn the number back into a name. So 0 equals 'rock', 1 equals 'Spock'. Or 'lizard' equals 2. You get the picture. Honestly, it took me a few times to read the instructions before I understood what they wanted. The basic steps were "Write a function to turn a name into a number". To me initially I thought this was just a variable with a value:
rock = 0
Spock = 1
But then I realized this wouldn't work, as you would not be able to turn the numbers back into a name because can't use numbers as a variable name. So I reread the steps again. I went back through some notes and it dawned on me that I could just take an argument into the function, and then
return the result. Which was another lightbulb moment for me! I love those moments.
The Return of the King
I've always known returning a value was different than printing a value. But I never really got it. I just pushed through. But this simple mini-lab made me realize how returning a value affects other code and those results. Once I got it, I smiled and felt good. I literally felt that this was a bigger lesson for me than the actual program itself.
With this newfound knowledge, I was able to better understand the first function and convert a name to number. Just a simple if/elif/else statement, with a return of the name it corresponded to, was all I needed. After I wrote the function, I took it into the test template they provided and ran it. The results were exact. So now, writing the function code to convert the number back to a name was merely child's play. After I wrote the functions, I went back and read the instructions for these two steps. I was able to better comprehend what they were intending me to do. Now that I was better understanding what they wanted, I pressed ahead.
Next step, write a function that took the return of these two functions and have it out put the desired results. But this was more involved that a simple 'this equals that'. This had computation, math, variables, logic, and flow control. Truthfully, I was nervous about it. While I've done all of these and understand them, I haven't done them all in one function and have it flow based upon certain conditions. But all I did was go back into the instructions and the code template and did one step at a time.
Since there are only 5 choices, I had to have a randomized number for the computer's choice. To do this, I just used the random library(module?). I prefer to use
random.randint(0,4) but they wanted us to use
random.randrange(0,5). I don't think one is better than the other in this case, but still. Six one way, half a dozen the other? If you know why one should be used over the other, I'd love to know.
Now write out some code that prints out what the player chose, and then also what the computer chose. And remember, you can't concatenate a string with a number. So you turn the number into a string and go forth. Thankfully, the instructions really held your hand on what math to use. In this case it was modulus (%) of 5. They never said why to use 5, but I wasn't asking. So now I had user input of a number, the computer input of a number, and the difference between the two. Now I know which one should win.
diff = (comp_number - player_number) % 5 if diff == 1 or diff == 2: print ('Computer wins!') elif diff == 3 or diff == 4: print ('Player wins!') elif diff == 0: print ('Player and computer tie!')
Simple logic right? Now just run the code, it takes your input and compares it to a randomized selection for the computer, and outputs the results. Each time, the computer's choice will be different. Nice and tidy.
Walk before run
Even though this is very basic, toddler like even, I felt that it was a win for me. I saw my mistakes, corrected them, and learned from them. I learned a better understanding on what returning a result means. It's times like this that make me feel giddy as a school boy that was able to spell dictionary just by sounding out the letters in my head. So it just goes to show me that I still can learn new tricks. Albeit more slowly at times, it still sinks in.
And yes, this entire exercise was because the professors (and I) watch the Big Bang Theory and enjoy it immensely.
If you want to look at the code itself, check it out in my github.
This weeks challenge is playing with a small graphical library for creating frames, buttons, messages, etc. for us to practice creating handlers and events that trigger them.