TicTacToe.py

--Originally published at Quirino´s Projects

Made a TicTacToe in python

captura-de-pantalla-2016-09-09-a-las-7-39-59-a-m

NOTE: I do not recomend using this code since indenting in wordpress is a hard task, instead, download it from my GitHub repo;

https://github.com/QuirinoC/TicTacToe.py


'''
Juan Carlos Quirino Carrasco
-----------------------------------------------------
Tried to do all functions as simple as possible
Maybe later i will do a more beautiful change_turn(),
user_input() and win(),tie()
-----------------------------------------------------
You are free to user, change and everything, just dont
copy and present like yours, you can, but give credit
'''
def table():
#this function creates a 3x3 table
'''
- / - / -
- / - / -
- / - / -
'''
board = []
for i in range(3):
board.append(["-"]*3)
return board
def print_board(board):
'''This function prints the board in pretty formated way'''
for column in board:
print ()
for i in column:
print (i, end=" ")
print ("\n")
'''
This is incorrect since it would
print the same column 3 times
'''
#print (" ", board_[i] , end=" / ")
#print (board_[i] , end=" / ")
#print (board_[i])
#print()
def win(board):
global turn
'''
This function checks if someone wins
Must be called every time someone writes
'''
a = board[0][0]
b = board[0][1]
c = board[0][2]
d = board[1][0]
e = board[1][1]
f = board[1][2]
g = board[2][0]
h = board[2][1]
i = board[2][2]
#Add to test for not being '-' DONE
''' 0 1 2
0 a / b / c
1 d / e / f
2 g / h / i
'''
if \
(a == b and b == c and b != "-") or \
(d == e and e == f and e != "-") or \
(g == h and h == i and h != "-") or \
\
(a == d and d == g 
Continue reading "TicTacToe.py"

Calling functions [Built-In]

--Originally published at Quirino´s Projects

monty-pythone28099s-search-for-the-holy-grail-tim

You get to know the power of programming get you get to functions, functions are a very useful way to reuse code, or a easy way to save a few lines in your code


#In python3 even print is a function

print("Hello World")

A function takes an argument, or arguments and work with it.

#To call a function you just need to write its name
#And give enough arguments and the correct type
#print takes strings, variables
hellow = "World"
print ("Hello") #prints "Hello"
print (hellow) #prints "World"
#Functions like print work with different values, but there are some

#That need specific types like:

print (abs("Hola")) #Would cause error since it needs an int or float
print (int(["Hello",42])) #Error since int() doesn't take lists

 

#Lets review the sum() function
#sum() takes an iterable object and returns it sum. Example
moneys = [500,700,-330,24,-30]
print (sum(moneys))
#Where sum() is the function itself, and moneys its the argument
#this would print 864

This function comes included with with python, this means you don’t have to define it as you would with a user created like

#We will get in deep about user defined functions in "Creating functions"
def doubleSum(x,y):
#This funcion takes 2 numbers and returns its double
    return 2 * (x + y)
print doubleSum(5,6)

 

External Links:

[Really useful]

https://docs.python.org/3/library/functions.html


Python conventions (Zen of Python but others for other languages)

--Originally published at Quirino´s Projects


#Go ahead try this in your python3/2 console

import this

#Just go to shell enter python3/python2 and paste that

20140709_python_bytes_3-11-1

 

Write easy to read code

 

#Python comes helpful to teach you how to indent your code
#Since it won't run if you don't
#Indent makes it easy to understand nested code
if 1 == 1:
    for i in range(5):
            print ("This is the range of 5")
            for x in range (45):
                if i = 5:
                    print("We are in the range of 5 of the range of 5")
#Of course this is a pretty bad example but i think you get the drill
#With indentation you know what things belongs to where

Useful external sources:

7 ways to write beautiful code


Basic output (print)

--Originally published at Quirino´s Projects

The most useful way to interact with Python is with print() it lets you interact with the console as it shows u what is going on, i recommend copying this code then interacting with it to see what you can achieve or just read it and try yourself, there is extra documentation i found useful at the bottom

'''As you might see now the pseudo
tutorial will be all written in Python
This way you could just copy the code and test yourself'''
#The most basic thing we learn to print in Python is
#The one and only, the world famous Hello world
print ("Hello world")
'''In python 3.something print works as a function.
It takes one argument which can be a string like Hello world
or a variable as it could be'''
string_x = "Hello world"
print (string_x) #Prints Hello World
#[Note for Myself add a link to Basic types and their use]
'''As you can see im jumping from single line comments to multi
when its needed, "...because i can"-Ken Bauer'''
#You can print to the screen more types like integers or floats
int_x = 10 #Prints 10
float_x = 20.0 #Prints 20.0
#NOTE we are not using the same x or something each something_x
#Its used only for names [Note for self, add tips on calling things]
print (int_x) #Prints the int_x value
print (float_x) #Prints the float_x value
'''Sometimes you want to print something more than just the
value of something thats when concatenating comes useful'''
hello = "Hola"
world = "Mundo"
print (hello + world) #This is a bit problematic since it Prints
#HolaMundo together so we need
print (hello + " " + world )
'''If we would need to print a string with a lots of variebles in it
Continue reading "Basic output (print)"

Use of comments

--Originally published at Quirino´s Projects

Comments come very useful when it comes to writing your program, it helps you to do organized code and more easy to understand, for you, or who ever needs to evaluate your code, for example instead of naming a function.

Commentaries do not affect your code

 def a_function_that_adds_two_numbers_and_returns_its_double(x,y):
    pass
 

You could just do

 
#This function adds two numbers and returns its double
def add_double(x,y):
    pass
 

Even tho you know what your codes do, sometimes is useful to let others understand easier and faster what you are trying to to, or you just could forget what the function/method meant like me

Another useful tip for python are multi-line comments using

''' love sheep.

So do I. Terrific animals. Terrific.

trouble.

No, no trouble.

Except at shearing. They can play up a bit, then; can't they?'''

Unlike single line comments #, you need to close the ”’ or you will have problems

Like this:

''' This program is useful when you want to add two numbers.
It reads from the user 2 input and returns its sum
def add():
x = input("Please Give me a number: ")
y = input("Input another one: ")
return ("The sum of %s and %s is %s") % (x,y,x+y)
print add()

This would run generate the following error:

File “Mastery_Topics_Comments.py”, line 10

SyntaxError: EOF while scanning triple-quoted string literal

This would be the right way: (You could copy this code but i don’t know how to indent in wordpress editor) delete [TAB] and tab it yourself

''' This program is useful when you want to add two numbers. It reads from the user 2 input and returns its sum'''
def add():
[TAB]x = input("Please Give me a number: ")
[TAB]y = input("Input another one: ")
[TAB]return ("The sum of %s and %s is %s") % 
Continue reading "Use of comments"