We spent the morning talking about the blockly maze exercise we did yesterday, along with algorithms in general. It turns out that the "left hand rule" of maze solving basically says, if you can turn left, turn left, otherwise, if you can go forward, go forward, and worst case scenario, go right. This has a real-world application of being a way to find items in file systems that are structured like "trees". It makes me think I should mess around and try to write my own locate script to see if I can make it work.
After that, we paired off to today's exercise, in which we created a guessing game. Pair programming is something that I am very much not good at. I speak quickly, I get excited by ideas (and have the subsequent unintelligible verbal diarrhea), and I'm not used to thinking out loud in such a way that it is decipherable to anyone but myself. To anyone reading this with whom I have pair programmed or will pair program with in the future: my deepest and sincerest apologies.
The guessing game was pretty straightforward, where it was a game where the user guessed a random number between 1 and 100 and then would be told if their guess was right, too high or too low. Then, we added features to alert the user when their guess was out of range or if they entered a string. Determining whether or not the input was out of range was easy. Trying to figure out if it was actually a string was much more difficult.
We prompted the user for input using the command:
guess = raw_input("Guess a number between 1-100")Due to the way that python works, the raw_input function converts anything that is entered into a string, so if the user inputs a number, for instance, "67", we need to convert that number from a string to an integer. This is easy enough to do:
guess = int(guess)BUT, if the user entered something that wasn't a number, e.g., "poop", then doing guess = int(guess) is going to puke on you. The way that we solved for this was to write a function that compared the input to a string of all the arabic numerals to see if the input contained only numbers (and thus was able to successfully run guess = int(guess)). The function was actually pretty nifty:
Finally, we added the functionality to play another round and to also keep track of the high score. I want to play around with it a bit more to see if I can make it look better and perhaps function faster. And now I should probably go to sleep or pack since we're moving again during the day.
EDITED TO ADD:
I totally forgot that we talked about git. git is way better than cvs and apparently I was doing horrible, terrible, no good things in cvs before, so it's good that I'm now committed (HA, GIT it??????) to git. Tabula rasa, amirite? (I understand that that's not at all what is meant by that.)
2 comments:
cf. string constants: https://docs.python.org/2/library/string.html
OH MY GOD YES. I was so mad when I found out that there were built in ways to do that. My Fortran mentality is seriously impeding my python experience.
Post a Comment