Learning Python Console Games
Learning Python Console Games
FACULTY OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE
CSC 318 (STRUCTURED PROGRAMMING)
By:
2031700071
LAWRENCE FAVOUR TREASURE
TITLE PAGE
TABLE OF CONTENTS
PYTHON PROGRAMMING
PROJECT DEVELOPED
CHALLENGE ENCOUNTERED
CONCLUSION
REFERENCE
CHAPTER ONE
INTRODUCTION
Python Programming
Python is a general purpose, high-level, interpreted, interactive and object-oriented scripting
language created by Guido Van Rossum. The syntax and structure is extremely simple to read
and follow, most of which can be understood even if you do not know any programming.
The python interpreter is easily extended with new functions and data types implemented in C
or C++ (or other languages callable from C). Python is also suitable as an extension language for
customized applications. With python you can do GUI development, Web application, System
administration tasks, Data Analysis, Visualization and much more.
Most programming languages use certain characters or keywords to group statements like
below
do ... done
if ... fi
Python uses a different principle. Programs get structured through indentation, this means that
code blocks are defined by their indentation, i.e. all statements with the same distance to the
right belong to the same block of code and indentation is done by clicking on tab once or
clicking on space four times.
With the Python interactive interpreter, it is easy to check Python commands. The Python
interpreter can be invoked by typing the command "python" without any parameter followed
by the "return" key at the shell prompt and rings up the following prompt.
Fig. 1.0 PYTHON WINDOW
To invoke the interpreter with script, you need to save your python program in a file with an
extension .py and then run the below command to execute the program.
For example:
print("Hello, World!")
With my familiarity with the basics of python programming syntax, I was able to develop a
random number generator/quiz game. This is a python console application.
A random number generator is used to generate random numbers to create a list, based on your
specifications. The random numbers are interchanged randomly using the random.randint()
function.
This python console application is an app which asks the player to input his/her name, then asks
the player to guess a number, for the player to advance to the next level the player has to guess
the right number else he/she gets stuck in the 1st level. If the number which the user picks
matches the number that has been generated it displays, you guessed it. If the number is
picked by the player is less than the number generated by the computer it displays, guess too
low and if the number chosen by the user exceeds the number generated by the computer is
displays, guess too high.
The quiz game asks the user some familiar question, when the user selects an option it takes
user to the next question. At the end of the questioning it displays the options the user selected
and matches it with the appropriate options. If the option selected by the user is similar to the
option the system displays, it displays the user score and gives an option to play again or end
game.
import statement
random.randint() function
while statement
if statement
elif statement
else statement
break statement
The Import Statement
This is the 1st statement I used in my code. This fig.1.5 Program Interface
The imports a module called random.randint().which will help in generating random numbers.
This function stores the return value in number. The randint() function is provided by the
random module, so it has to be written as random,randint(),the randint() function returns a
random integer between the two integer arguments passed to it. It generates the secret
number the player is trying to guess.
Fig. 1.3 random.randint() Function
This function displays text on the screen and gets text from the keyboard. It is called the I/O .
Interface of my Project
CHALLENGES ENCOUNTERED
Indentation Error:
When coding I placed an indentation in the wrong place, and sometimes I left white spaces
between lines of code.
Solution: I made sure to avoid placing indentation in the wrong areas and also limited the
way I made the white spaces.
Fig.1.6 Indentation Error
Name Error:
During the course of my coding I made a lot of errors but one very important one is the
name error, i failed to name some objects while coding and thus it threw an object not
found error.
Solution:
I made sure to declare every class and object to avoid the name error.
CONCLUSION
My project has been successful in the area of generating random numbers for
my console application developed using Python programming language.
Python has made me a better programmer and I have been able to debug and
correct errors developed in the course of my project.