0% found this document useful (0 votes)
5 views21 pages

Project_CSC_(1)[1]

The document contains a Python code for an odd or even hand cricket game where players can choose to bat or bowl against a computer. It includes functions for user input, random number generation, and game logic to determine scores and outcomes. The game also features a scoring system that can be stored in a MySQL database.

Uploaded by

saivarun1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

Project_CSC_(1)[1]

The document contains a Python code for an odd or even hand cricket game where players can choose to bat or bowl against a computer. It includes functions for user input, random number generation, and game logic to determine scores and outcomes. The game also features a scoring system that can be stored in a MySQL database.

Uploaded by

saivarun1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CODE :

#ODD OR EVEN GAME


from time import sleep
import mysql.connector as m
from tabulate import tabulate
import random
def battinguser():
global sum2
global sum
sum=0
I='not out'
while I=='not out':
J=int(input('Choose a
number from 0 to 10: '))
if J in
[0,1,2,3,4,5,6,7,8,9,10]:
K=random.randint(0,10)
print('My Choice is
:',K)
print('\n')
if J==K:
print('Third Umpire
decision pending')
sleep(3.0)
I='out'
print('OUT')
else:
if J==0:
sum+=K
else:
sum+=J
return sum
def bowlinguser():
global sum2
global sum
sum=0
I='not out'
while I=='not out':
J=int(input('Choose a
number from 0 to 10: '))
if J in
[0,1,2,3,4,5,6,7,8,9,10]:
K=random.randint(0,10)
print('My Choice is
:',K)
print('\n')
if J==K:
print('Third Umpire
decision pending')
sleep(3.0)
I='out'
print('OUT')
else:
if K==0:
sum+=J
else:
sum+=K
return sum
def bowlingcomp():
global sum2
global sum
sum2=0
L='not out'
while L=='not out' and
sum2<=sum:
N=int(input('Choose a
number from 0 to 10: '))
if N in
[0,1,2,3,4,5,6,7,8,9,10]:
O=random.randint(0,10)
print('My Choice is
:',O)
print('\n')
if N==O:
print('Third Umpire
decision pending')
sleep(3.0)
L='out'
print('OUT')
else:
if N==0:
sum2+=O
else:
sum2+=N
return sum2
def battingcomp():
global sum2
global sum
sum2=0
L='not out'
while L=='not out' and
sum2<=sum:
N=int(input('Choose a
number from 0 to 10: '))
if N in
[0,1,2,3,4,5,6,7,8,9,10]:
O=random.randint(0,10)
print('My Choice is
:',O)
print('\n')
if N==O:
print('Third Umpire
decision pending')
sleep(3.0)
L='out'
print('OUT')
else:
if O==0:
sum2+=N
else:
sum2+=O
return sum2
def game():
global sum2
global sum
global Name
global Score
global status
print('''
MENU
1)Play The Game
2)View The Score Table
3)Exit''')
def choice():
global Choice
Choice=int(input('Enter
Your Choice:'))
choice()
if Choice==1:
print('\n')
print('*'*160)
print('\n')
print('
WELCOME TO THE GAME OF HANDCRICKET
')
print('\n'*2)
print('Hi! My Name is PC')
List=[]
Number=int(input('Enter the
number of players:'))
print('-'*80)
print('\n')
for i in range(1,Number+1):
print('PLAYER',i)
Name=input('Enter your
Name:')
def match():
global List
global Name
global Score
global status
print('Hi,',Name,'!!!!')
start=input('Can we
begin the game. If yes, enter the
key S:')
print('\n')
if
start.lower()=='s':

print('============================
==========TOSS=====================
=================')
A=input('Choose
odd or even:')
M=A.lower()
if M=='odd' or
M=='even':

B=int(input('Choose a number from 1


to 10: '))
if B in
[1,2,3,4,5,6,7,8,9,10]:

C=random.randint(1,10)

print('My Choice
is :',C)

print('\n')
D=B+C
if D
%2==0:

E='even'

print('It is even')
if
E==M:

G=input('What do you choose Batting


or Bowling: ')

print('\n')

print('============================
=====FIRST
INNINGS============================
======')

H=G.lower()

if H=='batting':
sum=battinguser()

print('The total score is',sum)

print('The target for me is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is Time for me Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=battingcomp()

sleep(2.0)

if sum2<sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2>sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum

elif H=='bowling':
sum=bowlinguser()

print('The total score is',sum)

print('The target for you


is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is time for you to


Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=bowlingcomp()

sleep(2.0)

if sum2>sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2<sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum2
else:

F=random.choice(['Bat','Bowl'])

print('I choose to',F)

print('\n')

print('============================
=====FIRST
INNINGS============================
======')

if F=='Bat':

sum=bowlinguser()

print('The total score is',sum)

print('The target for you


is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is Time for you to


Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=bowlingcomp()

sleep(2.0)

if sum2>sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'


elif sum2<sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum2

else:

sum=battinguser()

print('The total score is',sum)

print('The target for me is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is time for me to


Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'sS':

sum2=battingcomp()

sleep(2.0)

if sum2<sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'


elif sum2>sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum
elif
(D+1)%2==0:

E='odd'

print('It is odd')
if
E==M:

G=input('What do you choose Batting


or Bowling:')

print('\n')

print('============================
=====FIRST
INNINGS============================
======')

H=G.lower()

if H=='batting':

sum=battinguser()

print('The total score is',sum)

print('The target for me is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is Time for me Bat')


print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in'Ss':

sum2=battingcomp()

sleep(2.0)

if sum2<sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2>sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum

elif H=='bowling':

sum=bowlinguser()

print('The total score is',sum)

print('The target for you


is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')
print('Now, It is time for you to
Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=bowlingcomp()

sleep(2.0)

if sum2>sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2<sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum2

else:

F=random.choice(['Bat','Bowl'])

print('I choose to',F)

print('\n')

print('============================
=====FIRST
INNINGS============================
======')

if F=='Bat':
sum=bowlinguser()

print('The total score is',sum)

print('The target for you


is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is Time for you to


Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=bowlingcomp()

sleep(2.0)

if sum2>sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2<sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum2
else:

sum=battinguser()

print('The total score is',sum)

print('The target for me is',sum+1)

print('\n')

sleep(2.0)

Time=input('Half Time!!! If you are


ready enter s:')

print('Now, It is time for me to


Bat')

print('\n')

print('============================
=====SECOND
INNINGS============================
=====')

if Time in 'Ss':

sum2=battingcomp()

sleep(2.0)

if sum2<sum:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'

elif sum2>sum:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

print('\n')

print('*'*160)

print('\n')

Score=sum
else:

print('Choose a number less than


10')
else:
print('Run
the program again and choose anyone
from odd or even')

if sum2==sum:
print('***SUPER
OVER***')
sleep(3.0)
U=input('Half
Time!!!IF ARE READY ENTER S :')
V=U.lower()
if V=='s':

P=input('Choose Batting or
Bowling :')
Q=P.lower()
q=0
if
Q=='batting':
dif=0
T='not
out'
while
T=='not out' and q<6:

R=int(input('Choose a number from 1


to 10 :'))
if
R in [0,1,2,3,4,5,6,7,8,9,10]:

S=random.randint(0,10)

print('My Choice
is :',S)

if R==S:

print('Third Umpire decion


pending')

sleep(3.0)

T='out'

print('OUT')

else:
if R==0:

dif+=S

else:

dif+=R

q+=1

print('Total Score is',dif)

print('Target for me is',dif+1)

sleep(2.0)

time=input('Half Time!!!If you are


ready enter s :')

print('Now, It is time for me to


Bat')
q=0
if time
in 'Ss':

dif2=0

T='not out'

while T=='not out' and q<6 and


dif2<dif:

R=int(input('Choose a number from 1


to 10 :'))

if R in [0,1,2,3,4,5,6,7,8,9,10]:

S=random.randint(0,10)

print('My Choice
is :',S)

if R==S:

print('Third Umpire decion


pending')

sleep(3.0)

T='out'
print('OUT')

else:

if S==0:

dif2+=R

else:

dif2+=S

q+=1
if
dif2>dif:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

else:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'


elif
Q=='bowling':
T='not
out'
while
T=='not out' and q<6:

R=int(input('Choose a number from 1


to 10 :'))
if
R in [0,1,2,3,4,5,6,7,8,9,10]:

S=random.randint(0,10)

print('My Choice
is :',S)

if R==S:

print('Third Umpire decion


pending')

sleep(3.0)

T='out'
print('OUT')

else:

if S==0:

sum+=R

else:

sum+=S
q+=1

print('Total Score is',dif)

print('Target for me is',dif+1)

sleep(2.0)

time=input('Half Time!!!If you are


ready enter s :')

print('Now, It is time for you to


Bat')
q=0
if time
in 'Ss':

dif2=0

T='not out'

while T=='not out' and q<6 and


dif2<dif:

R=int(input('Choose a number from 1


to 10 :'))

if R in [0,1,2,3,4,5,6,7,8,9,10]:

S=random.randint(0,10)

print('My Choice
is :',S)

if R==S:

print('Third Umpire decion


pending')

sleep(3.0)
T='out'

print('OUT')

else:

if R==0:

dif2+=S

else:

dif2+=R

q+=1
if
dif2<dif:

print('Yah!!! I WON the Game')

status='Lost the match with PC'

else:

print('Congratulations!!! You WON


the Game')

status='Won the match with PC'


if status=='Lost
the match with PC':
print('''Want
to Try Again
1)Try Again
2)Leave''')

choi=int(input('Enter your
choice :'))
print('-'*80)
print('\n')
if choi==1:
match()
elif choi==2:
pass
match()

List.append([Name,Score,status])
sleep(2.0)

X=[]
Y=[]
con=m.connect(host='localhost',user
='root',password='123456')
cur=con.cursor()
curs=con.cursor()
cur.execute("CREATE
DATABASE IF NOT EXISTS ALS")
cur.execute("USE ALS")
cur.execute("CREATE TABLE
IF NOT EXISTS Score_Table(Name
varchar(100),Highest_Score
int,Status varchar(250))")
cur.execute("SELECT * FROM
Score_Table")
data=cur.fetchall()
for p in List:
Y.append(p[0])
for q in data:
X.append(q[0])
for x in Y:
if x in X:
for i in List:
n=i[0]
s=i[1]
st=i[2]
if n==x:
for j in
data:
if
j[0]==n:

cur.execute("UPDATE Score_Table SET


Highest_Score={} WHERE
Name='{}'".format(s,n))

cur.execute("UPDATE Score_Table SET


Status='{}' WHERE
Name='{}'".format(st,n))
con.commit()
for x in Y:
if x not in X:
for i in List:
n=i[0]
s=i[1]
st=i[2]
if n==x:

cur.execute("INSERT INTO
Score_Table VALUES('{}',
{},'{}')".format(n,s,st))
con.commit()
print('''What Do You Want
To Do:
1)Back To Menu
2)Exit''')
ch=int(input('Enter your
choice:'))
print('*'*160)
print('\n')
if ch==1:
game()
else:
print("BYE!!! LET US
PLAY NEXT TIME")
elif Choice==2:

con=m.connect(host='localhost',user
='root',password='123456')
curs=con.cursor()
curs.execute("CREATE
DATABASE IF NOT EXISTS ALS")
curs.execute("USE ALS")
curs.execute("CREATE TABLE
IF NOT EXISTS Score_Table(Name
varchar(100),Highest_Score
int,Status varchar(200))")
curs.execute("SELECT * FROM
Score_Table ORDER BY Status
DESC,Highest_Score DESC")
data1=curs.fetchall()

table1=[['Name','Highest_Score','
Status']]

table2=[['Name','Highest_Score','
Status']]
for a in data1:
b=list(a)
if b[2]=='Won the match
with PC':
table1.append(b)
elif b[2]=='Lost the
match with PC':
table2.append(b)
print('\n')
print('
Table: Score_Win')

print(tabulate(table1,headers='firs
trow',tablefmt='fancy_grid'))
print('\n')
print('
Table: Score_Lost')
print(tabulate(table2,headers='firs
trow',tablefmt='fancy_grid'))
print('\n')
print('*'*160)
print('\n')
print('''What Do You Want
To Do:
1)Back To Menu
2)Exit''')
ch=int(input('Enter your
choice:'))
print('\n')
if ch==1:
game()
else:
print("BYE!!! LET US
PLAY NEXT TIME")
elif Choice==3:
print('\n')
print("BYE!!! LET US PLAY
NEXT TIME")
else:
print("Enter the right
option")
choice()
game()

You might also like