Monday, 25 January 2016

Tic Tac Toe Game

Hello friends,
Today I came with a complicated program.
You all know about the game tic tac toe.
After a lot of hard work I made this program.
Description:-
It is a two player game.
You can choose any symbol to play.
You can play a number of times in the same python shell.
So let's check out my effort,

print "       Tic  Tac  Toe" 
"This program is created by Sanjay Majhi."
print"""Rules for playing this game:
At first decide who is player 1
and who is player two.
Now start putting O and X in boxes
one by one and
if X or O met diagonally then won."""
def tictactoe():
    P1=raw_input("Player 1 enter your name:")
    P2=raw_input("Player 2 enter your name:")
    X=raw_input("player 1 choose X or O:")
    O=raw_input("Player 2 choose X or O:")
    table=""" 1 | 2 | 3
___|___|___
4 | 5 | 6
___|___|___
7 | 8 | 9
   |   |   """
    print table
    i=1
    while i<=9:
        if i%2!=0 or i==9:
            a=raw_input("Player 1 enter the coloumn number:-")  
            table=table.replace(a,X)
            print table
            if (table[1]==X and table[5]==X and table[9]==X) or (table[25]==X and table[29]==X and table[33]==X) or (table[49]==X and table[53]==X and table[57]==X) or (table[1]==X and table[29]==X and table[57]==X) or (table[9]==X and table[29]==X and table[49]==X) or (table[9]==X and table[33]==X and table[57]==X) or (table[5]==X and table[29]==X and table[53]==X) or (table[1]==X and table[25]==X and table[49]==X):
                print P1,"won"
                break
            elif i==9:
                print "Game had been tie."
                break
            else:
                pass
        else:
            b=raw_input("Player 2 your turn enter the coloumn number:-")
            table=table.replace(b,O)
            print table
            if (table[1]==O and table[5]==O and table[9]==O) or (table[25]==O and table[29]==O and table[33]==O) or (table[49]==O and table[53]==O and table[57]==O) or (table[1]==O and table[29]==O and table[57]==O) or (table[9]==O and table[29]==O and table[49]==O) or (table[9]==O and table[33]==O and table[57]==O) or (table[5]==O and table[29]==O and table[53]==O) or (table[1]==O and table[25]==O and table[49]==O):
                print P2,"won"
                break
            else:
                pass
        i+=1
tictactoe()
again=(raw_input("Do you want to play again:")).lower()
if again=="yes":
    tictactoe()
else:
    quit()

Hope you will like the game.
You can edit it to make it more helpful.
Get in touch with my blog to get new programs.

Program To Print A Pyramid

Hello friends,
Today I made a new program which prints a pyramid of given length.
In my previous post a I told you about how to print a hollow pyramid, and today I made some changes in the program to print a filled pyramid.
So,  let's check out,

def pyramid(len):
    spac_len=len
    pyramid_width=1
    i=1
    while i<=len:
        #First determine spacing then pyramid width
        print " "*spac_len,str(i)*pyramid_width
        pyramid_width+=2
        spac_len-=1
        i+=1
len=input("Enter lenth of pyramid:-")
pyramid(len)

The program is somewhat complicated for a beginner but you will understand.
Get in touch with my blog to get new programs.

Program To Print Factorial Of Any Number

Hello friends,
Today I came with a basic program to find factorial of any number.
Factorial means multiplying all the numbers upto a particular number.
For example:-
5!=5*4*3*2*1=120
So, Just check out

# Python program to find the factorial of a number provided by the user.
def factorial():
    # take input from the user
    num = int(input("Enter a number: "))
    factorial = 1

    # check if the number is negative, positive or zero
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)
times=input("Enter how many number you enter:-")
for i in range(times):
    factorial()

You can use it find factor very large number. 

Thanks for seeing this blog, if you like please share this blog.

Program To Print A Hollow Pyramid

Hello friends,
I am back with an interesting program which can print an hollow pyramid.
This pyramid can be of any number of lines.
But after 10 lines the pyramid's shape will change due to inclusion of two digit numbers.
So,  Just check out,

# This Program is created by Sanjay Majhi.
def hollow_pyramid(n):
    s=" "
    print n*s,sym
    n-=1
    p=1
    for i in range(1,n+1):
        if n!=1:
            print n*s,sym+(p*s)+sym # concatenation is done to correct the spacing.
        else:
            print n*s,sym*(p+2)
        p+=2
        n-=1
n=input("Enter a number:-")
sym=raw_input("Enter a symbol:-")
hollow_pyramid(n)

Hope that you will understand the program.
Please share this post if you like it.
Get in touch with my blog to get more Python Programs.

In my next post you can see program to print filled pyramid. 

Program To Count Number Of Vowels

Hello friends,
Today I came with a new program to count number of vowels in a word or statement.
This program is very easy to understand and you can customize it to produce more   output.
So, let's start,

# This program is created by Sanjay Majhi.
def vowels(word1):
    vowels=consonant=0
    import string
# We have to lower case the word.
    word2=word1.lower()
    for i in word2:
        if i=="a" or i=="e" or i=="i" or i=="o" or i=="u":
            vowels+=1
        else:
            consonant+=1
    else:
        print 'Number of vowels is', vowels
        print "Number of consonants is",consonant
N=input("Enter how many times you want to enter;")
for i in range(N):
    word=raw_input("Enter a word:")
    vowels(word)

Just copy the code and paste it in python shell and run.
If you like the program do not forget to comment.
Get in touch with my blog to get more programs.