Today’s thrilling Python romp: Pet Picking

So I’ve been running through the Python exercises on CodingBat for the last several days, and they’re fly as hell. This amazing site is proof that you don’t need a lot of flashy fancy gifs and pretty colors to make a sweet learning site. (Looking at you, Codecademy.)

Anyway, I started to run into warmup exercises (!!) that I couldn’t crack. Some of these were legit hard for me, but some of them were simply outside of my current range, so to speak. So I added a nice -1 to that len(experience) and looped back to my trusty NoStarch Python Crash Course to figure out what I’d missed.

The first thing I made was a nice list of animals that would make good pets, as per an exercise on page 60 in chapter 4.

|pets = [‘dog’,’cat’,’rabbit’,’snail’,’tortoise’,’mongolian death |worm’,’bigfoot’,’plesiosaur’,’mothman’,’jersey devil’]

Then I printed them all with a for loop. Still no biggie, still nothing I don’t know how to do. This was the extent of the exercise as the book outlined it.

|pets = [‘dog’, ‘cat’, ‘rabbit’, ‘snail’, ‘tortoise’, ‘mongolian death worm’, ‘bigfoot’, |’plesiosaur’, ‘mothman’, ‘jersey devil’]
|     for pet in pets:
|          print(‘A ‘+ pet + ‘ would make a great pet!’)
|     print(‘\nAll of these would make great pets!’)

The output was not unexpected.

|A dog would make a great pet!
|A cat would make a great pet!
|A rabbit would make a great pet!
|A snail would make a great pet!
|A tortoise would make a great pet!
|A mongolian death worm would make a great pet!
|A bigfoot would make a great pet!
|A plesiosaur would make a great pet!
|A mothman would make a great pet!
|A jersey devil would make a great pet!

|All of these would make great pets!

But then I thought, what if my user wanted to check and see if their preferred pet was on the list? I need them to understand that a bearded dragon does not count as a good companion animal, but that they ought to consider a plesiosaur.

My first step was to add another variable and make it an input. Here my sins began, because the book isn’t even through for loops yet and I’m not supposed to know about input. (By the way, I should have mentioned before that I’m working in Python 3.)

|want=input(‘What kind of pet would you want to get? ‘)

Yay, I have a new variable! Now I need to run through the list and check to see if the user’s pet request is an option.

Right away I ran into a problem because I can never for the life of me remember that you need two equals (==) to define a loop. After all those CodingBat exercises where I forgot this rule every single relevant time, you’d think I’d have broken this godawful habit. Someday.

So I added me an if inside my for, correct equals and all.

|pets = [‘dog’,’cat’,’rabbit’,’snail’,’tortoise’,’mongolian death |worm’,’bigfoot’,’plesiosaur’,’mothman’,’jersey devil’]
|want=input(‘What kind of pet would you want to get? ‘)
|     for pet in pets:
|          if want == pets[pet]:
|          print(‘A ‘+ pet + ‘ would make a great pet!’)
|#print(‘\nAll of these would make great pets!’)

I kept the last statement because apparently I hoard code like I hoard everything else in my life: for no reason.

Problems abounded! first of all, the search stopped at the first index. Second, Python threw a tantrum because strings aren’t integers and integers aren’t strings, and list item indices are DEFINITELY not strings. (Try counting to “banana.”) Finally, there was no way to deal with requests for pets that weren’t on the approved list. If my user can’t settle for a bigfoot, my user isn’t isn’t getting an animal, period!

Here’s what I figured out.

|pets = [‘dog’, ‘cat’, ‘rabbit’, ‘snail’, ‘tortoise’, ‘mongolian death worm’, ‘bigfoot’, |’plesiosaur’, ‘mothman’, ‘jersey devil’]
|want=input(‘What kind of pet would you want to get? ‘)
|     for pet in range(len(pets)):
|        if want == pets[x]:
|           print(‘A ‘+ want + ‘ would make a great pet!’)
|           break
|        if want != pets[x]:
|            x=x+1
|        if x == len(pets):
|            print(‘A ‘+ want + ‘ would just eat your homework.’)

I admit that I was continuing to cheat here, since I’m not supposed to know about break yet. However, I needed it and this was an urgent pressing question that had to be answered one way or the other, plus I didn’t want to sound like a manic robot screaming “A CAT WOULD MAKE A GREAT PET!” over and over again. There’s probably another, better way to do it. When I’m a more capable coder I’ll look back at this post and have a hearty laugh before deleting it in embarrassment.

For now, the first if looks to see if the input matches the index we’re on. If it doesn’t, it triggers the next if, which just pushes the index count variable to the next integer and keeps us marching onward to the end of the list. Finally, when x is as long as the length of the list, (which is to say we’re past the last actual list item because indicies are magic -1,) then Python can let the user down easy.


|What kind of pet would you want to get? a cat
|A a cat would just eat your homework.

Curses. Any deviation from “cat” or “dog” or “plesiosaur” as input resulted in pet denial and sometimes ridiculous output, even if it were a logical answer that otherwise fit the parameters of the list’s intent. This would not do!

The easiest step was to make sure that the user didn’t get denied based on capitalization. I .lowered() that boom and then applied a nice .strip(‘a ‘).

|pets = [‘dog’, ‘cat’, ‘rabbit’, ‘snail’, ‘tortoise’, ‘mongolian death worm’, ‘bigfoot’, |’plesiosaur’, ‘mothman’, ‘jersey devil’]
|want=input(‘What kind of pet would you want to get? ‘)
|if want[0:2] == ‘a ‘:
|    want=want.strip(‘a ‘)
|for pet in range(len(pets)):
|    if want == pets[x]:
|        print(‘A ‘+ want + ‘ would make a great pet!’)
|        break
|    if want != pets[x]:
|        x=x+1
|    if x == len(pets):
|        print(‘A ‘+ want + ‘ would just eat your homework.’)


|What kind of pet would you want to get? A Mothman
|A mothman would make a great pet!

This, of course, is fine. However, it would be nice to allow users to define their own lists of appropriate pets so that parents can be superusers who make sure that their kids, as regular users, are ordering proper domestic skinks and bonobos and golden hinds and whatnot. I could do this by simply asking the superuser for input and appending each input item to the pets list for the users, but I think I’d rather avoid having the user type out the various animals they want their kids to own. Instead, I want them to be able to enter an admin mode with a password and select pet options from a menu. That’s probably going to be my next modification. After that, I want to…

  • Create a returning user function for the kid
  • Allow the parent to modify a responsibility index based on how well they feel the kid has been taking care of the pet
  • Use calendar functions to determine how much keeping the pet has cost since last login
  • Use these factors to create a dashboard for the parent so that they can figure out if the kid should be allowed to get another animal.

It would be nice to make a fully fleshed-out pet keeping game out of this, but right now I have absolutely no idea how to make graphics. Maybe I could create a Rogue-style adventure. No game has really done it for me since Nethack and come to think of it, pet ownership is a significant part of that too. Maybe I’ll just code my own Nethack, but make it all about the pets.

Pokemon. I just came up with Rogue-style Pokemon. Fuck me. I’m going to go code some more.

Eventually I’ll get back to the book.

Saturday Fast Rec: Python For Kids

It’s Saturday! That means I recommend a book that I think you’ll like. (Yes, you!)

Today, I’m recommending Python for Kids: A Playful Introduction to Programming by Jason Briggs from No Starch Press. And I’m recommending it…for adults!

That’s right! If you (yes, you!) want to learn how to code, Python is a great place to start. It’s versatile, easy to learn (relatively speaking,) and professional coders do use it in actual applications. Kids can absolutely learn to make basic programs using the fun and surprisingly practical projects in this book, but I’m not ashamed to admit that this is where I started my own coding journey…at the age of 30. Remember that Jeopardy! champ who taught himself everything using children’s books? It’s not a bad way to introduce yourself to something new. Personally, I’ve not only learned to love Python thanks to Python for Kids, but found myself well prepared for the infamously difficult Java classes at my college because I’d already learned a similar programming language.

This book is fun, hands-on, and wonderful for all ages. There are some knockoff programming for kids books from other publishing houses, but don’t settle for them. No Starch Press is the best. This book is exactly where to start for kids or adults, and there are follow-up books that will rocket your skills to the upper atmosphere, if not to the moon. I recommend!