Thursday, 3 October 2013

First Impressions of Python

Today, our professor gave us an introduction to the Python programming language. At the end of class he issued us a challenge with to complete with our basic knowledge. The challenge is to modify a python program to be a small number guessing game. The program will select a random number between 1 and 100 and will prompt the user to guess the number. After each guess the user will be notified on whether their guess was too high, too low or correct. They will be allowed a total of 15 guesses before the program will give up. These were the instructions:

# Modify this script to be a number-guessing
# game.
# 1. The secret number should be random (1-100)
# 2. Input should be handled if it's not numeric
# 3. The user should be asked to guess until they
#    get the right number.
# 4. When the user guesses the right number, the
#    number of guesses should be printed
# Bonus: Make the script give up if the number hasn't
#        been guessed in 15 tries.

To begin, I cloned my professors git directory from his PC onto a remote machine I have access to.
At first I tried to remember how my professor had completed the task just moments ago but remembering code after seeing it only once is rather difficult. So I began researching and through that I began to learn about the different objects in Python and how they could help me.

For example, by importing the sys object classes, I could exit the script immediately (which you'll see in my script in a moment). I learned how to concatenate integer data type and string data types by converting numbers into strings using the str() function. Finally, I learned a little bit of Python syntax which I find much easier and more intuitive than older languages such as BASH.

Here is my version of the guessing game script.

At the end of all my git commits to save my progress, I had made a bunch of messages to myself. I need to make sure I make these more descriptive of what I was actually doing.

107adfd Added comments
ce0c668 completely finished
b2b3e08 working, added message to tell user how many guesses they took
7e8dea2 working, added mean message to tell the player they suck
3d6ff5f got loop working, now to get answers working
c164e98 Tried with correct try syntax
2812667 Working on it
45f5480 Fixed guess
a276cf5 Almost done I think
a22a590 Fixed while
57f732e Initial commit after first set of changes
f26a783 Added instructions
84158b5 Initial commit


Post a Comment