Minor Project Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Department of c

computer science and Enginneering


nginneering

GOVERNMENT POLYTECHNIC DARBHANGA

BIHAR, 846005

SESSION:2020-23

A PROJECT REPORT
ON
FANTASY CRICKET GAME
USING PYTHON

GUIDED BY:- SUBMITTED BY:-


BY:

D.N SINGH SIR ABHISHEK KUMAR (511141820031)


Dept of CSE ADITYA ANAND (511141820608)
(51114182060
ANANT KISHOR (511141820612)
(511
ACKNOWLEDGMENT

This project is done by as a semester minor project, as a part course


titled “FANTASY CRICKET GAME USING PYTHON”.

We are really thankful to our course the Principal Dr. KK PATHAK and
the HOD D.N SINGH, Computer Science and Engineering
Department, Government Polytechnic Darbhanga for his invaluable
guidance and assistance, without which the accomplishment of the
task would have never been possible.

World and realize the interrelation without which a Project can never
progress. In our present project we have chosen the topic- “FANTASY
CRICKET GAME USING PYTHON”.

We are also thankful to parents, friend and all staff of Computer


Science and Engineering, for providing us relevant information and
necessary clarifications, and great support.

Name of guide Name of HOD


(D.N singh) (D.N Singh)

External examiner Dr. K.K. Pathak


Principal
# -*-
coding:
utf-8 -
*-

# Form implementation generated from reading ui file 'main.ui'


#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(900, 675)
MainWindow.move(100,10)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
spacerItem = QtWidgets.QSpacerItem(250, 20, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setEnabled(False)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.horizontalLayout_5.addWidget(self.label_4)
self.e1 = QtWidgets.QLineEdit(self.centralwidget)
self.e1.setEnabled(False)
self.e1.setObjectName("e1")
self.horizontalLayout_5.addWidget(self.e1)
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setEnabled(False)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.horizontalLayout_5.addWidget(self.label_6)
self.e2 = QtWidgets.QLineEdit(self.centralwidget)
self.e2.setEnabled(False)
self.e2.setObjectName("e2")
self.horizontalLayout_5.addWidget(self.e2)
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setEnabled(False)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.horizontalLayout_5.addWidget(self.label_7)
self.e3 = QtWidgets.QLineEdit(self.centralwidget)
self.e3.setEnabled(False)
self.e3.setObjectName("e3")
self.horizontalLayout_5.addWidget(self.e3)
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setEnabled(False)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.horizontalLayout_5.addWidget(self.label_8)
self.e4 = QtWidgets.QLineEdit(self.centralwidget)
self.e4.setEnabled(False)
self.e4.setObjectName("e4")
self.horizontalLayout_5.addWidget(self.e4)
spacerItem1 = QtWidgets.QSpacerItem(250, 20, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem1)
self.horizontalLayout.addLayout(self.horizontalLayout_5)
self.verticalLayout.addLayout(self.horizontalLayout)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.verticalLayout.addWidget(self.line)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem2)
self.verticalLayout_8 = QtWidgets.QVBoxLayout()
self.verticalLayout_8.setObjectName("verticalLayout_8")
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout_8.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setTitle("")
self.groupBox.setFlat(False)
self.groupBox.setCheckable(False)
self.groupBox.setObjectName("groupBox")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.rb1 = QtWidgets.QRadioButton(self.groupBox)
self.rb1.setObjectName("rb1")
self.horizontalLayout_4.addWidget(self.rb1)
self.rb2 = QtWidgets.QRadioButton(self.groupBox)
self.rb2.setObjectName("rb2")
self.horizontalLayout_4.addWidget(self.rb2)
self.rb3 = QtWidgets.QRadioButton(self.groupBox)
self.rb3.setObjectName("rb3")
self.horizontalLayout_4.addWidget(self.rb3)
self.rb4 = QtWidgets.QRadioButton(self.groupBox)
self.rb4.setObjectName("rb4")
self.horizontalLayout_4.addWidget(self.rb4)
self.rb1.toggled.connect(self.ctg)
self.rb2.toggled.connect(self.ctg)
self.rb3.toggled.connect(self.ctg)
self.rb4.toggled.connect(self.ctg)
self.verticalLayout_8.addWidget(self.groupBox)
self.list1 = QtWidgets.QListWidget(self.centralwidget)
self.list1.setAutoFillBackground(True)
self.list1.setStyleSheet("color: rgb(0, 0, 127);\n"
"font: 75 12pt \"MS Shell Dlg 2\";")
self.list1.setAutoScroll(True)
self.list1.setObjectName("list1")
self.list1.itemDoubleClicked.connect(self.removelist1)
self.verticalLayout_8.addWidget(self.list1)
self.btn1 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.btn1.setFont(font)
self.btn1.setObjectName("btn1")
self.verticalLayout_8.addWidget(self.btn1)
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setText("")
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_5.setObjectName("label_5")
self.verticalLayout_8.addWidget(self.label_5)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
self.verticalLayout_8.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3.addLayout(self.verticalLayout_8)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem3)
self.verticalLayout_9 = QtWidgets.QVBoxLayout()
self.verticalLayout_9.setObjectName("verticalLayout_9")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.verticalLayout_9.addWidget(self.label_2)
self.l1 = QtWidgets.QLabel(self.centralwidget)
self.l1.setEnabled(True)
font = QtGui.QFont()
font.setFamily("MS Shell Dlg 2")
font.setPointSize(12)
font.setBold(False)
font.setItalic(False)
font.setWeight(9)
self.l1.setFont(font)
self.l1.setAlignment(QtCore.Qt.AlignCenter)
self.l1.setObjectName("l1")
self.verticalLayout_9.addWidget(self.l1)
self.line_2 = QtWidgets.QFrame(self.centralwidget)
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.verticalLayout_9.addWidget(self.line_2)
spacerItem4 = QtWidgets.QSpacerItem(20, 3, QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Minimum)
self.verticalLayout_9.addItem(spacerItem4)
self.list2 = QtWidgets.QListWidget(self.centralwidget)
self.list2.setStyleSheet("color: rgb(0, 0, 127);\n"
"font: 75 12pt \"MS Shell Dlg 2\";")
self.list2.setObjectName("list2")
self.list2.itemDoubleClicked.connect(self.removelist2)
self.verticalLayout_9.addWidget(self.list2)
self.btn2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.btn2.setFont(font)
self.btn2.setObjectName("btn2")
self.verticalLayout_9.addWidget(self.btn2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap("dream1.png"))
self.label_3.setObjectName("label_3")
self.verticalLayout_9.addWidget(self.label_3)
self.horizontalLayout_3.addLayout(self.verticalLayout_9)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem5)
self.verticalLayout.addLayout(self.horizontalLayout_3)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 900, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionNew = QtWidgets.QAction(MainWindow)
self.actionNew.setObjectName("actionNew")
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave_Team = QtWidgets.QAction(MainWindow)
self.actionSave_Team.setObjectName("actionSave_Team")
self.actionRules = QtWidgets.QAction(MainWindow)
self.actionRules.setObjectName("actionRules")
self.actionInstructions = QtWidgets.QAction(MainWindow)
self.actionInstructions.setObjectName("actionInstructions")
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionNew)
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave_Team)
self.menuFile.addAction(self.actionQuit)
self.menuFile.triggered[QtWidgets.QAction].connect(self.menufunction)
self.menubar.addAction(self.menuFile.menuAction())
self.bat=0
self.bwl=0
self.ar=0
self.wk=0
self.avl=1000
self.used=0
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.customContextMenuRequested.connect(self.context_menu)

def retranslateUi(self, MainWindow):


_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Fantasy Cricket Game"))
self.label_4.setText(_translate("MainWindow", "Batsmen"))
self.label_6.setText(_translate("MainWindow", "Bowlers"))
self.label_7.setText(_translate("MainWindow", "All Rounders"))
self.label_8.setText(_translate("MainWindow", "Wicketkeepers"))
self.label.setText(_translate("MainWindow", "Player Categories"))
self.rb1.setText(_translate("MainWindow", "BAT"))
self.rb2.setText(_translate("MainWindow", "BOWL"))
self.rb3.setText(_translate("MainWindow", "AR"))
self.rb4.setText(_translate("MainWindow", "WK"))
self.btn1.setText(_translate("MainWindow", "Available Points : 1000"))
self.label_2.setText(_translate("MainWindow", "Selected Players"))
self.l1.setText(_translate("MainWindow", "Team_name"))
self.btn2.setText(_translate("MainWindow", "Points used : "))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionNew.setText(_translate("MainWindow", "NEW Team"))
self.actionOpen.setText(_translate("MainWindow", "OPEN Team"))
self.actionSave_Team.setText(_translate("MainWindow", "SAVE Team"))
self.actionQuit.setText(_translate("MainWindow", "EVALUATE Team"))

def menufunction(self, action):


txt=action.text()
if txt=="NEW Team":
self.bat=0
self.bwl=0
self.ar=0
self.wk=0
self.avl=1000
self.used=0
self.list1.clear()
self.list2.clear()
self.l1.setText("Team_name")
self.showstatus()
text, ok = QtWidgets.QInputDialog.getText(MainWindow, 'Fantasy Cricket
Game', 'Enter name of team:')
if ok:
self.l1.setText(str(text))
if txt=='SAVE Team':
selected=""
count=self.list2.count()
for i in range(count):
selected=selected+self.list2.item(i).text()
if i<count-1:
selected=selected+","
self.save_Team(self.l1.text(),selected,self.used)
if txt=="OPEN Team":
self.bat=0
self.bwl=0
self.ar=0
self.wk=0
self.avl=1000
self.used=0
self.list1.clear()
self.list2.clear()
self.l1.setText("Team_name")
self.showstatus()
self.open_Team()
if txt=="EVALUATE Team":
from evaluateTeam import Mini_Ui
Dialog = QtWidgets.QDialog()
ui = Mini_Ui()
ui.setupUi(Dialog)
ret=Dialog.exec()

def save_Team(self,nm,string,val):
if self.bat+self.bwl+self.ar+self.wk!=11:
self.showdlg("Insufficient players")
return
sql="INSERT INTO teams (name, players, value) VALUES
('"+nm+"','"+string+"','"+str(val)+"');"
try:
cur=conn.execute(sql)
conn.commit()
self.showdlg ("Team Saved successfully")
except:
self.showdlg ("error in operation")
conn.rollback()

def open_Team(self):
sql="select name from teams"
cur=conn.execute(sql)
teams=[]
for row in cur:
teams.append(row[0])
team, ok = QtWidgets.QInputDialog.getItem(MainWindow, "Fantasy Cricket Game",
"Choose a team", teams, 0, False)
if ok and team:
self.l1.setText(team)
sql1="select players, value from teams where name='"+team+"'"
cur=conn.execute(sql1)
row=cur.fetchone()
selected=row[0].split(',')
self.list2.addItems(selected)
self.used=row[1]
self.avl=1000-row[1]
count=self.list2.count()
for i in range(count):
player=self.list2.item(i).text()
sql="select ctg from stats where player='"+player+"'"
cur=conn.execute(sql)
row=cur.fetchone()
ctgr=row[0]
if ctgr=="BAT":self.bat+=1
if ctgr=="BWL":self.bwl+=1
if ctgr=="AR":self.ar+=1
if ctgr=="WK":self.wk+=1
self.showstatus()

def context_menu(MainWindow):
MainWindow.menu = QtWidgets.QMenu()

def fillList(self,ctgr):
if self.l1.text()=='Team_name':
self.showdlg("Enter name of team")
return
self.list1.clear()
cursor = conn.execute("SELECT player from stats where ctg='"+ctgr+"'")
for row in cursor:
selected=[]
for i in range(self.list2.count()):
selected.append(self.list2.item(i).text())
if row[0] not in selected:self.list1.addItem(row[0])

def ctg(self):
ctgr=''
if self.rb1.isChecked()==True:ctgr='BAT'
if self.rb2.isChecked()==True:ctgr='BWL'
if self.rb3.isChecked()==True:ctgr='AR'
if self.rb4.isChecked()==True:ctgr='WK'
self.fillList(ctgr)

def criteria(self,ctgr, item):


msg=''
if ctgr=="BAT" and self.bat>=5:msg="Batsmen not more than 5"
if ctgr=="BWL" and self.bwl>=5:msg="bowlers not more than 5"
if ctgr=="AR" and self.ar>=3:msg="Allrounders not more than 3"
if ctgr=="WK" and self.wk>=1:msg="Wicketkeepers not more than 1"
if msg!='' or self.avl<=0:
msg = 'You have exhausted your points'
self.showdlg(msg)
return False
if ctgr=="BAT":self.bat+=1
if ctgr=="BWL":self.bwl+=1
if ctgr=="AR":self.ar+=1
if ctgr=="WK":self.wk+=1
cursor = conn.execute("SELECT player,value from stats where
player='"+item.text()+"'")
row=cursor.fetchone()
self.avl=self.avl-int(row[1])
self.used=self.used+int(row[1])
return True

def showstatus(self):
self.e1.setText(str(self.bat))
self.e2.setText(str(self.bwl))
self.e3.setText(str(self.ar))
self.e4.setText(str(self.wk))
self.btn1.setText("Available Points : {}".format(self.avl))
self.btn2.setText("Points used : {}".format(self.used))

def removelist1(self, item):


ctgr=''
if self.rb1.isChecked()==True:ctgr='BAT'
if self.rb2.isChecked()==True:ctgr='BWL'
if self.rb3.isChecked()==True:ctgr='AR'
if self.rb4.isChecked()==True:ctgr='WK'
ret=self.criteria(ctgr, item)
if ret==True:
self.list1.takeItem(self.list1.row(item))
self.list2.addItem(item.text())
self.showstatus()

def showdlg(self, msg):


Dialog = QtWidgets.QMessageBox()
Dialog.setText(msg)
Dialog.setWindowTitle("Fantasy Cricket Game")
ret=Dialog.exec()

def removelist2(self, item):


self.list2.takeItem(self.list2.row(item))
cursor = conn.execute("SELECT player,value, ctg from stats where
player='"+item.text()+"'")
row=cursor.fetchone()
self.avl=self.avl+int(row[1])
self.used=self.used-int(row[1])
ctgr=row[2]
if ctgr=="BAT":
self.bat-=1
if self.rb1.isChecked()==True:self.list1.addItem(item.text())
if ctgr=="BWL":
self.bwl-=1
if self.rb2.isChecked()==True:self.list1.addItem(item.text())
if ctgr=="AR":
self.ar-=1
if self.rb3.isChecked()==True:self.list1.addItem(item.text())
if ctgr=="WK":
self.wk-=1
if self.rb4.isChecked()==True:self.list1.addItem(item.text())
self.showstatus()

if __name__ == "__main__":
import sqlite3
conn = sqlite3.connect('fantasy.db')
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
conn.close()
GOVERMENT POLYTECHIC
DARBHANGA
DARBHANGA, (846004)

SUMMER INTERNSHIP (6 WEEK)

SUBMITTED BY:-

 STUDENT NAME …………………………………………


…………………………………
 REGISTRATION NO ………………………………………
………………………………
 BOARD ROLL NO …………………………………………
…………………………………
 SESSION ……………………………………………
……………………………………………………
 SEMESTER ……………………………………………
………………………………………………...

SUBMITTED TO
SBTE , PATNA

Date :-
Module 2 Assignment Solution

Sample Solution
There is no single correct solution for this assignment. This is a sample solution.

Data field Data type Example Your reason for using this
identifier data type

player_name String Virat Kohli Names may contain letters


and any other characters.
country String India Country name may
contain letters and any
other characters.
fours Integer 12 Number of fours hit is an
integer, cannot be a
decimal
run_rate Float 2.5 Run rate is the runs
required per delivery. This
can have decimal values.
p_stats Dictionary p1={'name':'Virat Kohli', This is a collection of
'role':'bat', 'runs':112, '4':10, relevant player stats.
'6':0, 'balls':119, 'field':0}
team List india=["v.kohli",""m.s.dhoni", A changeable collection of
"y.singh","r.sharma","r.ashwi player names.
n",""a.rahane"]

Programming with Python


Module 3 Assignment

Solution
Functions for the module
def batscore(d):

name=d.get('name')
runs=d.get('runs')
balls=d.get('balls')
batscore=int(runs/2)
four=d.get('4')
six=d.get('6')

batscore=int(runs/2)
if batscore>=50: batscore+=5
if batscore>=100: batscore+=10

if runs>0:
sr=runs*100/balls
if sr>=80 and sr<100: batscore+=2
if sr>=100:batscore+=4

batscore=batscore+four
batscore=batscore+2*six

return {'name':name,'batscore':batscore}

def bowlscore(d):
name=d.get('name')
wkt=d.get('wkts')
balls=d.get('overs')
runs=d.get('runs')
bowlscore=wkt*10
if wkt>=3: bowlscore=bowlscore+5
if wkt>=5: bowlscore=bowlscore=bowlscore+10
if balls>0:
er=runs/overs
#print ("eco:",er)
if er<=2: bowlscore=bowlscore+10
if er>2 and er<=3.5: bowlscore=bowlscore+7
if er>3.5 and er<=4.5: bowlscore=bowlscore+4
return {'name':name,'bowlscore':bowlscore}

Script for importing the functions to calculate batting or bowling score assuming the functions
are stored in a file called functions.py. (You can also execute these statements in the
interpreter.)

from functions import batscore, bowlscore

l1=[{'name':'Virat Kohli', 'role':'bat', 'runs':112, '4':10, '6':0, 'balls':119, 'field':0},


{'name':'du Plessis', 'role':'bat', 'runs':120, '4':11, '6':2, 'balls':112, 'field':0}]

Programming with Python


Module 3 Assignment

l2=[{'name':'Bhuvneshwar Kumar', 'role':'bowl', 'wkts':1, 'overs':10, 'runs':71, 'field':1},


{'name':'Yuzvendra Chahal', 'role':'bowl', 'wkts':2, 'overs':10, 'runs':45, 'field':0},
{'name':'Kuldeep Yadav', 'role':'bowl', 'wkts':3, 'overs':10, 'runs':34, 'field':0}]

for i in l1:
print (batscore(i))

for i in l2:
print (bowlscore(i))

Programming with Python


Solution
class book:
def __init__(self,aut, pub, p, cp=0):
self.author=aut
self.publisher=pub
self.price=p
self.copies=cp
def get_author(self):
return self._author
def set_author(self, aut):
self._author=aut
return
author=property(get_author, set_author)
def get_publisher(self):
return self._publisher
def set_publisher(self, pub):
self._publisher=pub
return
publisher=property(get_publisher, set_publisher)
def get_price(self):
return self._price
def set_price(self,p):
self._price=p
return
price=property(get_price, set_price)
def get_copies(self):
return self._copies
def set_copies(self,cp):
self._copies=cp
return
copies=property(get_copies, set_copies)

def get_royalty(self):
if self.copies<=500:
self._royalty=self.copies*self.price*10/100
elif self.copies<=1000:
self._royalty=500*self.price*10/100+ \
(self.copies-500)*self.price*12.5/100
else:
self._royalty=500*self.price*10/100 + \
500*self.price*12.5/100 + \
(self.copies-1000)*self.price*15/100
return self._royalty

class ebook(book):
def __init__(self, aut, pub, p, cp=0, form=None):
super().__init__(aut,pub,p,cp)
self.format=form
def get_format(self):
return self._format
def set_format(self, form):
self._format=form
return
format=property(get_format, set_format)
def get_royalty(self):
ryl=super().get_royalty()
ryl=ryl-ryl*12/100
self._royalty=ryl
return self._royalty

if __name__== "__main__":
print ('print book example')
b1=book('aa','ss',100,600)
print ("Royalty earned : ", b1.get_royalty())
print ('ebook example')
e1=ebook('aa','ss',100, 3, 'PDF')
print ('Royalty earned' , e1.get_royalty())
Module 5 Assignment

Solution
Part 1: To create the required database and add data programmatically by using the Insert
query

import sqlite3
db=sqlite3.connect("m5assignment.db")
cur=db.cursor()
cur.execute('''CREATE TABLE books (
BookID INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT (50) NOT NULL,
author TET(20),
price REAL);''')
for x in range(5):

ttl=input("enter book's title: ")


aut=input("enter name of author: ")
pr=float(input("enter price: "))
sql="INSERT INTO books (title, author, price) VALUES
('"+ttl+"','"+aut+"','"+str(pr)+"');"

try:
cur=db.cursor()
cur.execute(sql)
db.commit()
print ("one record added successfully")
except:
print ("error in operation")
db.rollback()
db.close()

Part 2: To enter the title and number of copies and see the total cost

import sqlite3
db=sqlite3.connect("m5assignment.db")
cur=db.cursor()

total=0
while True:

ttl=input("enter book's title: ")

sql="SELECT * FROM books WHERE title='"+ttl+"'"


cur=db.cursor()
cur.execute(sql)
rec=cur.fetchone()
if rec!=None:
print (rec)
pr=rec[3]
qty=int(input("enter number of books purchased"))
cost=pr*qty
total=total+cost

Programming with Python


Module 5 Assignment

else:
print ("Title Not Found")
choice=input("add more books[Y/N]?")
if choice=='N': break
print ("Total cost of Purchased Books",total)
db.close()

Sample data in the database:

BookID Title Author Price


1 Learn Python 3 The Hard Way Zed A.Shaw 575
2 Think Python Allen B. Drowney 475
3 Data structures and algorithms in Goodritch 470
Python
4 Python Programming:A modular Taneja & Kumar 450
Approach
5 Python Machine Learning By Liu & Yuxi 725
Example
6 Core Python Programming R.Nageshwar Rao 599

Programming with Python


Module 2 Assignment

Introduction
For this assignment, read the scenario below and then respond to the problem statement
described.

Scenario
You are required to design the data structure to display the individual player stats for cricket
players. A player may have represented more than one team and may have played in more
than one format such as Test, ODI and T20.

Problem Statement
Create a list of different data fields and use appropriate Python data types to represent each
of them.

Assignment Submission
You can use the table format displayed below for your response. You can add additional
columns of information if you want. Create a document/pdf of your response and upload a zip
file using the Progress Tracker.

Data field Data type Example Your reason for using this data
identifier type

player_name String Virat Kohli Names may contain letters and


any other characters.

Learning outcomes being evaluated


• Distinguish between different data types.
• Use recommended conventions for naming variables.

Programming with Python


Module 3 Assignment

Introduction
For this assignment, read the scenario below and then respond to the problem statement
described.

Scenario
The 'Man of the Match' award of a 50-over cricket match is decided by computing points
earned by players. The points are calculated on the basis of the following rules:

Batting
● 1 point for 2 runs scored
● Additional 5 points for a half-century
● Additional 10 points for a century
● 2 points for strike rate (runs/balls faced) of 80-100
● Additional 4 points for strike rate>100
● 1 point for hitting a boundary (four) and 2 points for over boundary (six)

Bowling
● 10 points for each wicket
● Additional 5 points for three wickets in innings
● Additional 10 points for 5 wickets or more in innings
● 4 points for economy rate (runs given per over) between 3.5 and 4.5
● 7 points for an economy rate between 2 and 3.5
● 10 points for an economy rate less than 2

Fielding
● 10 points each for catch/stumping/run out

The performance of each player is stored in a dictionary object. Displayed below is data for 5
players.

p1={​'name'​:​'Virat Kohli'​, ​'role'​:​'bat'​, ​'runs'​:112, ​'4'​:10, ​'6'​:0,


'balls'​:119, ​'field'​:0}

p2={​'name'​:​'du Plessis'​, ​'role'​:​'bat'​, ​'runs'​:120, ​'4'​:11, ​'6'​:2,


'balls'​:112, ​'field'​:0}

p3={​'name'​:​'Bhuvneshwar Kumar'​, ​'role'​:​'bowl'​, ​'wkts'​:1, ​'overs'​:10,


'runs'​:71,​ 'field'​:1}

p4={​'name'​:​'Yuzvendra Chahal'​, ​'role'​:​'bowl'​, ​'wkts'​:2, ​'overs'​:10,


'runs'​:45,​ 'field'​:0}

p5={​'name'​:​'Kuldeep Yadav'​, ​'role'​:​'bowl'​, ​'wkts'​:3, ​'overs'​:10, ​'runs'​:34,


'field'​:0}

Programming with Python


Module 3 Assignment

Problem Statement
Assuming that these are the top 5 performers, write a Python program to decide the player
with the highest points. Develop separate functions to compute batting and bowling points
and save them in a module. These functions should be imported into the main code.

Assignment Submission
Your submission should have a fully functional code with:

1. One module containing the required functions.


2. One script file with the main code which computes the top player amongst the 5 given
players.

When your script is run, it should generate a result which might look like this:

{'name': 'Virat Kohli', 'batscore': 83}


{'name': 'du Plessis', 'batscore': 94}
{'name': 'Bhuvneshwar Kumar', 'bowlscore': 10}
{'name': 'Yuzvendra Chahal', 'bowlscore': 24}
{'name': 'Kuldeep Yadav', 'bowlscore': 42}
>>>

Hint
The calculation of points will require conditional logic to be applied. Since this is to be
performed for 5 players, there’s looping involved. Defining and calling functions from a
module is also required.

You can approach this assignment in the following sequence:

1. First, create the required variables.


2. Next, convert the rules into expressions and conditions. For example, points = runs/2
or if runs >50, points = points+2 and so on.
3. Next, define each of the two functions for batting or fielding and bowling.
4. Finally, write your program using the conditions and the functions.

Learning outcomes being evaluated


● Use conditional logic in Python programs
● Write programs using conditionals, loops, iterators and generators, functions and
modules and packages.

Programming with Python


Problem Statement
Design a ‘book’ class with title, author, publisher, price and author’s royalty as instance
variables. Provide getter and setter properties for all variables. Also define a method
royalty() to calculate royalty amount author can expect to receive the following royalties:10%
of the retail price on the first 500 copies; 12.5% for the next 1,000 copies sold, then 15% for
all further copies sold.

Then design a new ‘ebook’ class inherited from ‘book’ class. Add ebook format (EPUB, PDF,
MOBI etc) as additional instance variable in inherited class.
Override royalty() method to deduct GST @12% on ebooks
Module 5 Assignment

Introduction
In this assignment, you are required to apply your knowledge of connecting a SQLite
database to a Python program. Read the scenario below and then respond to the problem
statement described.

Scenario
In a bookstore, somewhere in India.
Pi: Hi, do you have Think Python?
Store Assistant: Let me check, Sir.
(The assistant checks on his computer. This is what his screen looks like.)

Store Assistant: Yes Sir, we have it in stock. It costs Rs. 475. Would you like to take it?
Pi: Yes please. And give me two copies.
Store Assistant: Sure. Anything else, Sir?
Pi: Nope thanks. What's the total?
(The assistant now enters the number of copies on his computer. This is what his screen looks like.)

Problem Statement
Assume that you have to create such an application for maintaining a database of book titles
and their costs.

Part 1: Write the script to create the required database and add data programmatically by
using the Insert query.

Part 2: Write a Python script connecting to the database created that has the following
features:

Programming with Python


Module 5 Assignment

• A books table having the title, author, and price as fields.


• Accept input from the user for the title and quantity purchased by the customer.
• Fetch the price from the table by executing the Select query.
• Calculate the total amount and display it.

Assignment Submission
Your submission should have fully functional code with:

1. One script file containing the code for create the required database and add data
programmatically by using the Insert query.
2. One script file with the main code which takes the book title (of a book you have
already entered into the database) and number of copies as input and displays the
output.
3. One database file containing data for the book id, title, author and price.

Learning outcomes being evaluated


• Create a SQLite database by executing the INSERT query.
• Execute SQL Queries through Python.

Programming with Python


PROJECT ON FANTASY CRICKET GAME

It is an online game where you create a virtual team of real cricket players
and score points depending on how your chosen players perform in real
life matches. To win a tournament, you must try and get the maximum
points and the No. 1 rank amongst other participants. Here's how a
Fantasy Cricket game may look like
Technology used:
1. Python (language)
. Basic knowledge (loops ,functions , connections
etc..)
. PYQT5 (install)
. QT designer (To make GUI- use of icons, boxes
etc…)
2. SQL or Excel Sheet for Database ( for connecting
data )
Data Base design:

i. First, create the database of players.

ii. Plan the required tables and add data to your


database.
iii. Next, create the GUI. Generate the required Python code for the
UI.
iv. Finally, populate the Python code generated in step ii
with more attributes and method definitions (action
listeners).

The following things required :-

Elements Categories Criteria


Application
Flow Populate left list widget from database as per category
radio button clicked
Add players from players list to selected players list
Remove players from selected players list to players list
Show error if selection criterion is violated
Calculate team score
UI Widgets should be placed symmetrically
Code
Naming Convention Meaningful variable names
Meaningful function names
Modularity A single function should be used for a single task/No
code repetition (make generalized functions)
Error Handling Implement error handling wherever required
Database
Design
Tables Number of tables
Column names
Data types
Data
ssss

You might also like