Day 21 (Making a Game)

Good morning blog! Today was no easier to get out of bed than the last, but we managed to pull ourselves together! I got a 90% sleep quality score last night, so I’m feeling pretty breezy! Today I’m doing some Python programming, and I just made a little game using if / elif statements and a library called “random” to make a guessing game. I don’t really post much code here, but here’s what I made:


from random import randint

random = randint(1,10)

guess = int(raw_input(“What’s your guess? Pick a number 1 – 10\n”))
if guess == random:
print “You got it!”
elif guess != random:
print “Wrong guess!”
print random


Quick rundown of the code (as I have no hashtags in there) is as follows. First line imports the “random” library which has the function of generating a random integer (randint). Second line assigns the variable “random” to the function “randint” from 1 to 10. Third line assigns the variable “guess” to raw_input (user will type in a number and that will be assigned to the variable). \n creates a new line. The next lines are where it gets kinda nifty, if they guess correctly the program will return “You got it!”, otherwise, if the guess isn’t the random integer, it will say “wrong guess!” I should probably add an “else” clause incase they input something like “Banana”. **edit, I just ran the script, and even if the user types a non-integer it still activates the elif statement because strings do not equal integers! Neat! **second edit, i took out the “int” around guess, but doing so assigned whatever I put in into a string, so int is very necessary.

Anyway, Zed said to make a game, and this is my game. Not really text adventure style like his example was, but I do that enough for one of my classes with quest. So there’s the end of chapter 31 of LPTHW! Only a few more lessons and then the guidances of Dr. Poulson ends, which I’m somewhat bummed about. Dr. Poulson’s videos got me over a huge mental gap and made me confident that the resources exist on the internet that if I ever get stuck (not really an if, more so a when) that I’ll be able to find the pertinent information to keep learning!