0% found this document useful (0 votes)
49 views44 pages

Merged PDF 1

The document describes a menu-driven banking management system using Python dictionaries. The system allows users to perform operations like creating new accounts, depositing and withdrawing money, querying account balances, viewing all accounts, and closing accounts. Functions are defined to handle each operation like create(), deposit(), withdrawal() etc. A main() function displays the menu and calls the appropriate function based on the user's choice. The account details are stored in a dictionary with account numbers as keys and name and balance as values.

Uploaded by

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

Merged PDF 1

The document describes a menu-driven banking management system using Python dictionaries. The system allows users to perform operations like creating new accounts, depositing and withdrawing money, querying account balances, viewing all accounts, and closing accounts. Functions are defined to handle each operation like create(), deposit(), withdrawal() etc. A main() function displays the menu and calls the appropriate function based on the user's choice. The account details are stored in a dictionary with account numbers as keys and name and balance as values.

Uploaded by

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

Page |1

C.S. PRACTICAL FILE


CLASS-12 [2023-2024]
[Q1] STRING QUESTIONS ->
#[Q-1] STRING MENU BASED PROGRAM-
while True:
l='~'*95
print('')
print('THESE OPTIONS CAN BE DONE TO A STRING #')
print('1 : COUNT NUMBER OF DIGITS,ALPHABETS AND SPECIAL CHARACTERS IN A STRING')
print('2 : CHECK IF ITS A PALINDROME STRING')
print('3 : CHANGE FIRST LETTER OF EVERY WORD IN SENTENCE TO CAPITALS')
print('4 : FIND A CHARACTER IN A STRING AND REPLACE WITH ANOTHER CHARACTER')
print('5 : COUNT NUMBER OF WORDS IN A STRING WHICH DO NOT CONTAIN ANY
VOWEL')
print('')
s=input('ENTER THE SENTENCE :')
print('')
ch=int(input('ENTER CHOICE :'))
print('')
def cchar(s):
d=0
a=0
sc=0
sym=['`','~','!','@','#','$','%','^','&','*','(',')','_','-
','+','=','{','[','}','}','|',":",';','"','<'',','>','.','?',"/"]
sen=s.split(' ')
for i in range (len(s)):
if (s[i].isdigit()):
d=d+1
Page |2

elif (s[i].isalpha()):
a=a+1
elif s[i] in sym:
sc=sc+1
print('No. of Digits =',d)
print('No. of Alphabets =',a)
print('No. of Special Characters =',sc)
def palincheck(s):
if s==(s[::-1]):
print('Its a Palindrome String')
else :
print('Its not a Palindrome String')
def capati(s):
print(s.title())
def replacer(s):
ow=input('You want to replace : ')
rw=input('New word you want : ')
print('')
print(s.replace(ow,rw))
def countnonvowel(s):
sen=s.split(' ')
vow=0
vowc=['a','e','i','o','u','A','E','I','O','U']
for i in sen:
nvow=0
for a in i:
if a in vowc:
nvow=nvow+1
else:
continue
if nvow>0:
Page |3

continue
else:
vow=vow+1
print('No. of words which have NO vowel =',vow)

if ch==1:
cchar(s)
print('')
print(l)
elif ch==2:
palincheck(s)
print('')
print(l)
elif ch==3:
capati(s)
print('')
print(l)
elif ch==4:
replacer(s)
print('')
print(l)
elif ch==5:
countnonvowel(s)
print('')
print(l)
OUTPUT: PART-1 =>
Page |4

PART-2:

PART-3:

PART-4:
Page |5

PART-5:

[Q2] LIST PROGRAMS =>


# [Q2] LIST MENU BASED PROGRAMS -
while True:
print('')
print('THESE OPTIONS CAN BE DONE TO A LIST')
print('1 : SWAP FIRST HALF OF THE LIST WITH SECOND HALF OF THE LIST')
print('2 : SWAP ADJACENT ELEMENTS OF THE LIST')
print('3 : REMOVE REPEATING ELEMENTS FROM THE LIST')
print('4 : COUNT THE STRINGS WHICH HAVE FIRST & LAST CHAR AS SAME & NO. OF
LETTERS ARE MORE THAN 3')
Page |6

print('5 : FROM A LIST OF STRINGS COUNT NO. OF PALINDRONE STRINGS ')


print('6 : SHIFT ALL ELEMENT OF A LIST TOWARD LEFT BY 1 STEP')
print('7 : SHIFT ALL ELEMENT OF A LIST TOWARDS RIGHT BY 1 STEP')
print('8 : PRINT ALL MAGIC NUMBERS')
l=[]
print('')
n=int(input('INPUT THE NUMBER OF ELEMENT YOU WANT TO ADD : '))
print('')
for i in range (0,n):
v=(input('INPUT THE VALUE : '))
l.append(v)
print('')
ch=int(input('ENTER YOUR CHOICE : '))
print('')
line='-'*95

print('')
def swap_first(l):
l[:len(l)//2],l[len(l)//2:]=l[len(l)//2:],l[:len(l)//2]
print(l)
return print

def swap_adjacent(l):
for i in range(0,len(l)-1,2):
l[i],l[i+1]=l[i+1],l[i]
print(l)

def remove_repeat(l):
l1=[]
for x in l:
if x not in l1:
Page |7

l1.append(x)
print(l1)
def count_fl_char(l):
for i in l:
if str(i).isnumeric()==True:
continue
else:
if len(i)>3:
if i[0]==i[-1]:
print(i,'HAS FIRST & LAST CHAR SAME AND HAS MORE THAN 3 LETTERS')

def count_palindrome(l):
for i in l:
if str(i).isnumeric():
n = str(i)
if len(str(i)) < 2:
continue
elif n[:len(n)] == n[len(n)::-1]:
print (f'{i} is a palindromic number')
else:
if i[:len(l)] == i[len(i)::-1]:
print (f'{i} is a palindrome')

def shift_left(l):
t=l[0]
for i in range (1,len(l)):
l[i-1]=l[i]
l[len(l)-1]=t
print(l)

def shift_right(l):
Page |8

t=l[len(l)-1]
for i in range(len(l)-2,-1,-1):
l[i+1]=l[i]
l[0]=t
print(l)
def magic_num(l):
import math
for i in l:
s=0
p=str(i)
if p.isnumeric():
for a in p:
f=math.factorial(int(a))
s=s+f
if str(s)==i:
print(i,'IS A MAGIC NUMBER')

if ch==1:
swap_first(l)
print('')
print(line)
elif ch==2:
swap_adjacent(l)
print('')
print(line)
elif ch==3:
remove_repeat(l)
print('')
print(line)
elif ch==4:
count_fl_char(l)
Page |9

print('')
print(line)
elif ch==5:
count_palindrome(l)
print('')
print(line)
elif ch==6:
shift_left(l)
print('')
print(line)
elif ch==7:
shift_right(l)
print('')
print(line)
elif ch==8:
magic_num(l)
print('')
print(line)
OUTPUT -> PART-1:

PART-2:
P a g e | 10

PART-3:

PART-4:
P a g e | 11

PART-5:

PART-6:

PART-7:
P a g e | 12

PART-8:

[Q3] MENU BASED QUESTIONS ->


#[Q3]MENU BASED RANDOM PROGRAMS ->
while True:
l='-'*95
import math as m
print('These are the following options available ->')
print('')
print('1 : Find sum of following series X^2/1!+X^4/3!+X^6/5! ..... upto n terms')
print('2 : To find first n terms of Fibonacci series')
print('3 : To show reverse of a number in Integer form')
print('4 : Take 2 numbers and show their HCF & LCM')
print('')
ch=int(input('Enter choice : '))
print('')
def fact(x):
P a g e | 13

f=1
while x > 0:
f = f*x
x -= 1
return f
def sumofseries():
x = int(input('Enter number: '))
n = int(input('Enter number of terms: '))
sum = 0
for i in range(n):
value = (x**(2*n)/m.factorial(2*n-1))
n -= 1
sum += value
print('')
print('Sum of series =',sum)
print('')
print(l)
print('')
def fibonacci():
n = int(input('Enter number of terms for series: '))
a=0
b=1
print('First',n,'terms of Fibonacci series ->')
print('')
for i in range(n):
print (a)
c=a+b
a=b
b=c
print('')
print(l)
P a g e | 14

print('')
def reversenumber():
n = int(input('Enter number to be reversed: '))
a = str(n)[::-1]
b = int(a)
print('Reversed Number =',b)
print('')
print(l)
print('')
def hcf(num1, num2):
if num1 > num2:
smaller = num2
if num1%smaller==0:
return smaller
else:
smaller = num1
if num2%smaller==0:
return smaller
for i in range(1, smaller//2 +1):
if num1 % i == 0 and num2 % i == 0:
hcf = i

return hcf
def lcm(num1, num2):
if num1 > num2:
greater = num1
else:
greater = num2
while True:
if greater % num1 == 0 and greater % num2 == 0:
lcm = greater
P a g e | 15

break
greater += 1
return lcm

if ch==1:
sumofseries()
elif ch==2:
fibonacci()
elif ch==3:
reversenumber()
elif ch==4:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print('')
hcf(num1,num2)
lcm(num1,num2)
print("The Highest common factor of", num1, "and", num2, "=", hcf(num1, num2))
print("The Lowest common multiple of", num1, "and", num2, "=", lcm(num1, num2))
print('')
print(l)
print('')

OUTPUT: PART-1=>
P a g e | 16

PART-2:

PART-3
P a g e | 17

PART-4:

[Q4]DICTIONARY BASED QUESTIONS ->


#[Q4] BANK MANAGING PROGRAM THRU MENU(DICTIONARIES)
account={1:['AKSHAN',1000],2:['MESHA ', 2500]}
a=' '
s=' '
d=' '
def create():
amo=int(input(' ACCOUNT NUMBER : '))
aname=input(' NAME : ')
bal=float(input(' BALANCE : '))
account[amo]=[aname,bal]
def deposit():
an=int(input(' ACCOUNT NUMBER :'))
if an not in account:
print(' INVALID ACCOUNT ENTERED')
return
P a g e | 18

damt=float(input(' MONEY YOU WANT TO DEPOSIT : '))


print(' OLD BALANCE - ',account[an][1])
account[an][1]+=damt
print(' NEW BALANCE - ',account[an][1])
def withdrawal():
an=int(input(' ACCOUNT NUMBER : '))
if an not in account:
print(' INVALID ACCOUNT ENTERED')
return
withbal=float(input(' AMOUNT YOU WANT TO WITHDRAW : '))
print(' OLD BALANCE - ',account[an][1])
account[an][1] -= withbal
print(' NEW BALANCE - ',account[an][1])

def query():
an=int(input(' ENTER ACCOUNT NO. YOU WANT TO CHECK : '))
if an in account :
print(' Account holder name : ',account[an][0])
print(' Account balance : ',account[an][1])
else:
print(' NO ACCOUNT FOUND')
def close():
an=int(input(' ACCOUNT YOU WANT TO CLOSE :'))
account.pop(an)
def allaccdetails():
for an in account:
print(' Account number - ',an)
print(' Name - ',account[an][0])
print(' Balance - ',account[an][1])

def main() :
P a g e | 19

while True :
print('')
print('~'*45)
print('TASKS PERFORMABLE # ')
print('')
print(s,'1 : CREATE/ADD')
print(s,'2 : DEPOSIT')
print(s,'3 : WITHDRAWAL')
print(s,'4 : QUERY')
print(s,'5 : VEIW ALL ACCOUNT DETAILS')
print(s,'6 : CLOSE')
print('')
print('~'*45)
ch=int(input('\t ENTER CHOICE : '))
print('')
if ch==1:
create()
elif ch==2:
deposit()
elif ch==3:
withdrawal()
elif ch==4:
query()
elif ch==5:
allaccdetails()
elif ch==6:
close()

while True:
print('\t\tWELCOME TO THE BROKE BANK')
main()
P a g e | 20

OUTPUT: PART-1 =>

PART-2:

PART-3
P a g e | 21

PART-4:
P a g e | 22

PART-5:

PART-6
P a g e | 23
# Q6 para.txt MORE TEXT FILE COUNT RELATED FUNCTIONS

# To count number of words starting with 'A'


def countA():
with open('para.txt','r') as f:
a=f.readlines()
for i in range(len(a)):
a[i]=a[i].rstrip('\n')
cnt=0
for i in a:
b=i.split()
for c in b:
if c[0]=='A':
cnt=cnt+1

print("NO. of words starting with 'A' :",cnt)

#TO count number of words that have length greater than 10


def count_10():
with open('para.txt','r') as f:
a=f.readlines()
for i in range(len(a)):
a[i]=a[i].rstrip('\n')
cnt=0
for i in a:
b=i.split()
for c in b:
if len(c)>10:
cnt=cnt+1

print("NO. of words with length > 10 :",cnt)

# TO count occurence of word 'the'


def cthe():
with open('para.txt','r') as f:
a=f.readlines()
for i in range(len(a)):
a[i]=a[i].rstrip('\n')
words=''
for i in a:
words+=i+' '
total=len(words)-len(a)
words=words.split(' ')
words.pop()
print()
cnt=0
for i in words:
if i=='the':
cnt=cnt+1
print("'the' OCCURES",cnt,'TIMES')

def main():
while True:
print('YOUR CHOICES ARE ->')
print("1 - To count number of words starting with 'A' ")
print('2 - TO count number of words that have length > 10')
print("3 - TO count occurence of word 'the'")
ch=int(input('ENTER CHOICE :'))
if ch==1:
countA()
elif ch==2:
count_10()
elif ch==3:
cthe()
print()

main()
'''
OUTPUT =>
para.txt :

I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
YOUR CHOICES ARE ->
1 - To count number of words starting with 'A'
2 - TO count number of words that have length > 10
3 - TO count occurence of word 'the'
ENTER CHOICE :1
NO. of words starting with 'A' : 3

YOUR CHOICES ARE ->


1 - To count number of words starting with 'A'
2 - TO count number of words that have length > 10
3 - TO count occurence of word 'the'
ENTER CHOICE :2
NO. of words with length > 10 : 1

YOUR CHOICES ARE ->


1 - To count number of words starting with 'A'
2 - TO count number of words that have length > 10
3 - TO count occurence of word 'the'
ENTER CHOICE :3

'the' OCCURES 2 TIMES

'''
# Q6 para.txt COUNT RELATED FUNCTION ON LINE BASIS

# To count number of line starting with 'T'


def countlineT():
with open('para.txt','r') as f:
a=f.readlines()
for i in range(len(a)):
a[i]=a[i].rstrip('\n')
cnt=0
for i in a:
if i[0]=='T':
cnt=cnt+1
print("NO. of lines starting with 'T' :",cnt)

# To count number of line containing 'is'


def countis():
with open('para.txt','r') as f:
a=f.readlines()
for i in range(len(a)):
a[i]=a[i].rstrip('\n')
cnt=0
for i in a:
j=i.split(' ')
for k in j:
if k == 'is':
cnt=cnt+1
break
print(" 'is' OCCURES IN -",cnt,'LINES')

def main():
while True:
print('OPTIONS ARE ->')
print("1 - To count number of line starting with 'T' ")
print("2 - To count number of line containing 'is' ")
ch=int(input('ENTER CHOICE :'))
if ch==1:
countlineT()
elif ch==2:
countis()
print()

main()

'''
OUTPUT :

para.txt

I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
aliscyinto is nothing but hoax
Is quite there _ is it good for study

OPTIONS ARE ->


1 - To count number of line starting with 'T'
2 - To count number of line containing 'is'
ENTER CHOICE :1
NO. of lines starting with 'T' : 1

OPTIONS ARE ->


1 - To count number of line starting with 'T'
2 - To count number of line containing 'is'
ENTER CHOICE :2
'is' OCCURES IN - 8 LINES
'''
# Q8 Remove all line cointaing 'Z' from para.txt and inserting that line into newfile.txt
import os
def remZ():
line=[]
f1=open('para.txt','r')
f2=open('temp.txt','w')
f3=open('newfile.txt','w')
l=f1.readlines()
for i in l:
for j in i:
if j=='Z':
f3.write(i)
break
else :
f2.write(i)
break
f1.close()
f2.close()
f3.close()
os.remove('para.txt')
os.rename('temp.txt','para.txt')

remZ()

'''
OUTPUT ->

original para.txt

I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
ZOLO is nice ZOHO is also great
aliscyinto is nothing but hoax
Is quite there _ is it good for study
zam is zam

new para.txt

I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
aliscyinto is nothing but hoax
Is quite there _ is it good for study
zam is zam

newfile.txt this contains all lines which contain 'Z'

ZOLO is nice ZOHO is also great

'''
# Q9 File for creation of sports related info storing

def createfile():
f=open('sports.txt','a')
l=[]
while True:
sportid=input('ENTER SPORT ID :')
sname=input( 'ENTER SPORT NAME :')
fee= (input('ENTER FEE :'))
timing=input( 'ENTER TIMINGS :')
rec=sportid+"\t"+sname+"\t"+fee+"\t"+timing+"\n"
l.append(rec)
c=input('CONTINUE =')
if c=='n':
break
f.writelines(l)

def printinfo():
f=open('sports.txt','r')
l=f.readlines()
print()
print("SPORTS ID\t SPORTSNAME\t FEES\t TIMINGS")
for i in l:
print(i)
f.close()

def main():
while True:
print('1- Make text file sports.txt which contains sports information')
print('2- Read file sports.txt')
ch=int(input('ENTER CHOICE :'))
if ch==1:
createfile()
if ch==2:
printinfo()

main()

"""
OUTPUT ->

1- Make text file sports.txt which contains sports information


2- Read file sports.txt
ENTER CHOICE :1
ENTER SPORT ID :1
ENTER SPORT NAME :FOOTBALL
ENTER FEE :300
ENTER TIMINGS :7:00 AM - 9:00 AM
CONTINUE =Y
ENTER SPORT ID :2
ENTER SPORT NAME :BASKETBALL
ENTER FEE :250
ENTER TIMINGS :9:15 AM - 11:00 AM
CONTINUE =3
ENTER SPORT ID :3
ENTER SPORT NAME :TENNIS
ENTER FEE :500
ENTER TIMINGS :10:00 AM - 1:00 PM
CONTINUE =Y
ENTER SPORT ID :4
ENTER SPORT NAME :CRICKET
ENTER FEE :150
ENTER TIMINGS :4:30 PM - 6:00 PM
CONTINUE =Y
ENTER SPORT ID :5
ENTER SPORT NAME :BADMINTON
ENTER FEE :200
ENTER TIMINGS :6:30 PM - 8:00 PM
CONTINUE =n
1- Make text file sports.txt which contains sports information
2- Read file sports.txt
ENTER CHOICE :2

SPORTS ID SPORTSNAME FEES TIMINGS


1 FOOTBALL 300 7:00 AM - 9:00 AM
2 BASKETBALL 250 9:15 AM - 11:00 AM

3 TENNIS 500 10:00 AM - 1:00 PM

4 CRICKET 150 4:30 PM - 6:00 PM

5 BADMINTON 200 6:30 PM - 8:00 PM

"""
# Q10 To create a binary file student.dat and do the functions

import pickle
def writeline():
f=open('student.dat','ab')
while True:
rno=int(input('ENTER ROLL NUMBER :'))
name=input( 'ENTER NAME :')
mks=float(input( 'ENTER MARKS :'))
rec=[rno,name,mks]
pickle.dump(rec,f)
c=input('CONTINUE -')
if c=='n':

break
f.close()

def readbinfile():
with open('student.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
print(rec)
except EOFError:
break

def mkscondition():
with open('student.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
if rec[2]>90:
print(rec)
except EOFError:
break
def main():
print('THESE ALL THINGS CAN BE DONE TO STUDENT FILE')
print('1 - ADD RECORD')
print('2 - DISPLAY ALL RECORDS')
print('3 - DISPLAY RECORD WHERE MARKS>90')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
writeline()
if ch==2:
readbinfile()
if ch==3:
mkscondition()

main()
"""
OUTPUT :

THESE ALL THINGS CAN BE DONE TO STUDENT FILE


1 - ADD RECORD
2 - DISPLAY ALL RECORDS
3 - DISPLAY RECORD WHERE MARKS>90
ENTER CHOICE :1
ENTER ROLL NUMBER :1
ENTER NAME :AKSHAN
ENTER MARKS :90
CONTINUE -y
ENTER ROLL NUMBER :2
ENTER NAME :FEDRIK
ENTER MARKS :39
CONTINUE -y
ENTER ROLL NUMBER :3
ENTER NAME :TEE MEN
ENTER MARKS :79
CONTINUE -y
ENTER ROLL NUMBER :4
ENTER NAME :LYLA
ENTER MARKS :92
CONTINUE -n
ENTER CHOICE :2
[1, 'AKSHAN', 90.0]
[2, 'FEDRIK', 39.0]
[3, 'TEE MEN', 79.0]
[4, 'LYLA', 92.0]
ENTER CHOICE :3
[4, 'LYLA', 92.0]

"""
# Q 11 TO edit the student.dat file for following functions
import pickle
import os
def changemks():
f1=open('student.dat','rb')

f2=open('temp.dat','wb')
rno=int(input("ENTER STUDENT ROLL NUMBER :"))
nmks=int(input('ENTER NEW MARKS :'))
while True:
try:
rec=pickle.load(f1)
for i in rec:
if rec[0]==rno:
rec[2]=nmks
pickle.dump(rec,f2)
except EOFError:
break
f1.close()
f2.close()
os.remove('student.dat')
os.rename('temp.dat','student.dat')

def delete():
f1=open('student.dat','rb')
f2=open('temp.dat','wb')
r=int(input('ENTER ROLL NUMBER THAT NEEDS TO BE DELETED - '))
while True:
try:
rec=pickle.load(f1)
if rec[0]!=r:
pickle.dump(rec,f2)
except EOFError:
break
f1.close()
f2.close()
os.remove('student.dat')
os.rename('temp.dat','student.dat')

def readbinfile():
f=open('student.dat','rb')
rec=[]
while True:
try:
rec=pickle.load(f)
print(rec)
except EOFError:
break

def main():
print('1 - UPDATE MKS OF SPECIFIC STUDENT USING ROLL NUMBER {USING TEMP.DAT}')
print('2 - DELETE RECORD OF SPECIFIC STUDENT {USING TEMP.DAT}')
print('3 - DISPLAY ALL RECORDS')
print('4 - EXIT')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
changemks()
elif ch==2:
delete()
elif ch==3:
readbinfile()
elif ch==4:
break

main()

"""
OUTPUT :

1 - UPDATE MKS OF SPECIFIC STUDENT USING ROLL NUMBER {USING TEMP.DAT}


2 - DELETE RECORD OF SPECIFIC STUDENT {USING TEMP.DAT}
3 - DISPLAY ALL RECORDS
4 - EXIT
ENTER CHOICE :3
[1, 'AKSHAN', 90.0]
[2, 'FEDRIK', 39.0]
[3, 'TEE MEN', 79.0]
[4, 'LYLA', 92.0]
ENTER CHOICE :1
ENTER STUDENT ROLL NUMBER :1
ENTER NEW MARKS :98
ENTER CHOICE :3
[1, 'AKSHAN', 98]
[2, 'FEDRIK', 39.0]
[3, 'TEE MEN', 79.0]
[4, 'LYLA', 92.0]
ENTER CHOICE :2
ENTER ROLL NUMBER THAT NEEDS TO BE DELETED - 3
ENTER CHOICE :3
[1, 'AKSHAN', 98]
[2, 'FEDRIK', 39.0]
[4, 'LYLA', 92.0]

"""
# Q12 CERATE BOOK DATABASE AND TO DISPLAY ALL RECORDS
import pickle
def bookcreation():
f=open('books.dat','ab')
l=[]
while True:
bookno=input( 'ENTER BOOK NUMBER :')

name=input( 'ENTER BOOK NAME :')


subject=input( 'ENTER BOOK SUBJECT:')
price=int(input('ENTER BOOK PRICE :'))
rec=[bookno,name,subject,price]
l.append(rec)
c=input('CONTINUE -')
if c=='n':
break
pickle.dump(l,f)
f.close()

def readbook():
with open('books.dat','rb')as f:
while True:
try:
l=pickle.load(f)
print('BookNumber','Name','Subject','Price')
for i in l:
print(i[0],i[1],i[2],i[3])
except EOFError:
break
def main():
print('1 - ADD RECORDS')
print('2 - DISPLAY ALL RECORDS')
print('3 - EXIT')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
bookcreation()
elif ch==2:
readbook()
elif ch==3:
break

main()

"""
OUTPUT :

"""
# Q13 UPDATE BOOK DATABASE AND SEARCH SPECIFIC

# To edit book databse


import pickle
import os
def changeprice():
f1=open('books.dat','rb')
f2=open('temp.dat','wb')
no=input("ENTER BOOK NUMBER :")
np=int(input('ENTER NEW BOOK PRICE :'))
while True:
try:
rec=pickle.load(f1)
for i in rec:
if i[0]==no:
i[3]=np
pickle.dump(rec,f2)
except EOFError:
break
f1.close()
f2.close()
os.remove('books.dat')
os.rename('temp.dat','books.dat')

def pricecondition():
with open('books.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
for i in rec:
if i[3]>500:
print('BookNumber','Name','Subject','Price')
print(i[0],i[1],i[2],i[3])
except EOFError:
break

def readbook():
with open('books.dat','rb')as f:
while True:
try:
l=pickle.load(f)
print('BookNumber','Name','Subject','Price')
for i in l:
print(i[0],i[1],i[2],i[3])
except EOFError:
break

def main():
print('1 - UPDATE PRICE OF SPECIFIC BOOK')
print('2 - SHOW BOOKS WITH PRICE>500')
print('3 - READ ALL')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
changeprice()
elif ch==2:
pricecondition()
elif ch==3:
readbook()
print()

main()

'''
OUTPUT :

1 - UPDATE PRICE OF SPECIFIC BOOK


2 - SHOW BOOKS WITH PRICE>500
3 - READ ALL
ENTER CHOICE :3
BookNumber Name Subject Price
1 Will action 250
2 today fictionb 300
3 QWERTY science 1000
4 Longing biography 350

ENTER CHOICE :2
BookNumber Name Subject Price
3 QWERTY science 1000

ENTER CHOICE :1
ENTER BOOK NUMBER :1
ENTER NEW BOOK PRICE :666

ENTER CHOICE :2
BookNumber Name Subject Price
1 Will action 666
BookNumber Name Subject Price
3 QWERTY science 1000

ENTER CHOICE :3
BookNumber Name Subject Price
1 Will action 666
2 today fictionb 300
3 QWERTY science 1000
4 Longing biography 350

'''
# Q14 BINARY CINEMA DIRECTORY

# To create a cinema.dat using dictionary abd run queries

import pickle
import os
def createmovie():
with open('cinema.dat','ab') as f:
while True:
mno=int(input('ENTER MOVIE NUMBER :'))
mname=input( 'ENTER MOVIE NAME :')
mtype=input( 'ENTER MOVIE TYPE :')
c={'movno':mno,'movname':mname,'movtype':mtype}
pickle.dump(c,f)
con=input('CONTINUE :')
if con=='n':
break
print(c)

def readdict():
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print('MOVIE TYPE ->',m['movtype'])
print()
except EOFError:
break

def movietype():
movietype=input('ENTER MOVIE TYPE YOU WANT TO SEARCH :')
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
if m['movtype']==movietype:
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print()
except EOFError:
break

def moviesearch():
moviesearch=input('ENTER MOVIE YOU WANT TO SEARCH :')
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
if m['movname']==moviesearch:
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print('MOVIE TYPE ->',m['movtype'])
print()

except EOFError:
break

def main():
print('1 - DISPLAY DETAILS')
print('2 - SEARCH SPECIFIC MOVIE TYPE')
print('3 - SEARCH SPECIFIC MOVIE NAME')
print('4 - CREATE DATABASE OF MOVIES')
print('5 - EXIT')
print()
while True:
ch=int(input('ENTER CHOICE :'))
print()
if ch==1:
readdict()
elif ch==2:
movietype()
elif ch==3:
moviesearch()
elif ch==4:
createmovie()
elif ch==5:
break

main()
"""
OUTPUT :

1 - DISPLAY DETAILS
2 - SEARCH SPECIFIC MOVIE TYPE
3 - SEARCH SPECIFIC MOVIE NAME
4 - CREATE DATABASE OF MOVIES
5 - EXIT

ENTER CHOICE :4

ENTER MOVIE NUMBER :1


ENTER MOVIE NAME :TOMMOROW
ENTER MOVIE TYPE :FUTURE
CONTINUE :Y
ENTER MOVIE NUMBER :2
ENTER MOVIE NAME :HAUNT
ENTER MOVIE TYPE :HORROR
CONTINUE :Y
ENTER MOVIE NUMBER :3
ENTER MOVIE NAME :WILL HE LIVE?
ENTER MOVIE TYPE :DOCUMENTRY
CONTINUE :Y
ENTER MOVIE NUMBER :4
ENTER MOVIE NAME :THE TRAIN RIDER
ENTER MOVIE TYPE :THRILLER
CONTINUE :Y
ENTER MOVIE NUMBER :5
ENTER MOVIE NAME :QUANTAS
ENTER MOVIE TYPE :FUTURE
CONTINUE :Y
ENTER MOVIE NUMBER :6
ENTER MOVIE NAME :KILLMONGER
ENTER MOVIE TYPE :SCI-FI
CONTINUE :n
ENTER CHOICE :1

MOVIE NUMBER -> 1


MOVIE NAME -> TOMMOROW
MOVIE TYPE -> FUTURE

MOVIE NUMBER -> 2


MOVIE NAME -> HAUNT
MOVIE TYPE -> HORROR

MOVIE NUMBER -> 3


MOVIE NAME -> WILL HE LIVE?
MOVIE TYPE -> DOCUMENTRY

MOVIE NUMBER -> 4


MOVIE NAME -> THE TRAIN RIDER
MOVIE TYPE -> THRILLER

MOVIE NUMBER -> 5


MOVIE NAME -> QUANTAS
MOVIE TYPE -> FUTURE

MOVIE NUMBER -> 6


MOVIE NAME -> KILLMONGER
MOVIE TYPE -> SCI-FI

ENTER CHOICE :2

ENTER MOVIE TYPE YOU WANT TO SEARCH :FUTURE


MOVIE NUMBER -> 1
MOVIE NAME -> TOMMOROW

MOVIE NUMBER -> 5


MOVIE NAME -> QUANTAS

ENTER CHOICE :3

ENTER MOVIE YOU WANT TO SEARCH :WILL HE LIVE?


MOVIE NUMBER -> 3
MOVIE NAME -> WILL HE LIVE?
MOVIE TYPE -> DOCUMENTRY
"""
# Q15 [ CSV ]

# Making a csv file and entering game data for student

import csv
def wcsv():
with open ('student_game.csv','w') as f:
fields=['St_id','St_name','Game_name','Result']
w=csv.writer(f)
w.writerow(fields)
while True:
ID=int(input('Enter student ID :'))
nm=input( 'Enter student name :')
gn=input( 'Enter game name :')
rt=input( 'Enter game result :')
r=[ID,nm,gn,rt]
w.writerow(r)
c=input('Continue ->')
if c=='N' or c=='n':
break

def rcsv():
with open ('student_game.csv','r') as f:
r=csv.reader(f)
for i in r:
print(i[0],i[1],i[2],i[3])

def main():
while True:
print('1 - Enter record')
print('2 - Read record')
ch=int(input('Enter choice =>'))
if ch==1:
wcsv()
elif ch==2:
rcsv()

main()

'''
OUTPUT ->

1 - Enter record
2 - Read record
Enter choice =>1
Enter student ID :1
Enter student name :AKSHAN
Enter game name :GOLF
Enter game result :WIN
Continue ->Y
Enter student ID :2
Enter student name :TANI
Enter game name :BASKETBALL
Enter game result :DRAW
Continue ->Y
Enter student ID :3
Enter student name :KUSH
Enter game name :FOOTBALLL
Enter game result :LOST
Continue ->Y
Enter student ID :4
Enter student name :ANDY
Enter game name :CHESS
Enter game result :DRAW
Continue ->Y
Enter student ID :5
Enter student name :LINSY
Enter game name :FENCING
Enter game result :WIN
Continue ->N

1 - Enter record
2 - Read record
Enter choice =>2
St_id St_name Game_name Result
1 AKSHAN GOLF WIN
2 TANI BASKETBALL DRAW
3 KUSH FOOTBALLL LOST
4 ANDY CHESS DRAW
5 LINSY FENCING WIN

'''
import mysql.connector as mc
mydb=mc.connect(host='localhost',user='root',passwd='password')
mycursor=mydb.cursor()
mycursor.execute('show databases')
for i in mycursor:
print(i)

def create():
mycursor.execute('create database if not exists stock')
mycursor.execute('use stock')
mycursor.execute('create table if not exists ITEM (ICODE varchar(5) primary key,Idesc varchar(10),Price numeric(8,2), Purdate date)')

def insertrecord():
mycursor.execute("use stock")
while True:
icode=input ('ENTER ITEM CODE :')
ides=input ('ENTER ITEM DESCRIPTION :')
p=int(input ('ENTER ITEM PRICE :'))
purdate=input('ENTER PURCHASE DATE [YYYY-MM-DD] :')
mycursor.execute("insert into item values(%s,%s,%s,%s);",(icode,ides,p,purdate))
mydb.commit()# TO SAVE CHANGES IN DATA BASE
c=input("CONTINUE :")
if c=='n' or c=='N':
break
def showall():
mycursor.execute('use stock')
mycursor.execute('select * from item')
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])

def showgreater_1000():
mycursor.execute('use stock')
mycursor.execute('select * from item where price > 1000')
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])

def priceincrease():
mycursor.execute('use stock')
record=input('ENTER ITEM CODE :')
priceinc=int(input('PRICE INCREASE BY :'))
mycursor.execute(f"update item set price=price+{priceinc} where icode={record};")
mydb.commit()# TO SAVE CHANGES IN DATA BASE

def newcolumn():
mycursor.execute('use stock')
mycursor.execute("alter table item add (DISCOUNT numeric(25,10) default 100 );")
mydb.commit()# TO SAVE CHANGES IN DATA BASE

def startp():
mycursor.execute('use stock')
mycursor.execute("select * from item where ides like 'P%';")
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])

def main():
while True:
print('1 - CREATE')
print('2 - WRITE')
print('3 - SHOW ALL RECORDS')
print('4 - SHOW ITEM WITH PRICE > 1000')
print('5 - INCREASE PRICE BY X FOR SPECIFIC RECORD')
print('6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }')
print('7 - SHOW WHERE IDES START WITH P')

ch=int(input('ENTER CHOICE :'))


if ch==1:
create()
elif ch==2:
insertrecord()
elif ch==3:
showall()
elif ch==4:
showgreater_1000()
elif ch==5:
priceincrease()
elif ch==6:
newcolumn()
elif ch==7:
startp()
main()
'''
OUTPUT =>

1 - CREATE
2 - WRITE
3 - SHOW ALL RECORDS
4 - SHOW ITEM WITH PRICE > 1000
5 - INCREASE PRICE BY X FOR SPECIFIC RECORD
6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }
ENTER CHOICE :6
1 - CREATE
2 - WRITE
3 - SHOW ALL RECORDS
4 - SHOW ITEM WITH PRICE > 1000
5 - INCREASE PRICE BY X FOR SPECIFIC RECORD
6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }
ENTER CHOICE :3
('1', 'food', Decimal('200000.00'), datetime.date(2019, 8, 2), Decimal('100.0000000000'))
('2', 'phone', Decimal('700.00'), datetime.date(2006, 9, 2), Decimal('100.0000000000'))
('3', 'calculator', Decimal('400.00'), datetime.date(2000, 1, 23), Decimal('100.0000000000'))
('4', 'burger', Decimal('30000.00'), datetime.date(2023, 2, 24), Decimal('100.0000000000'))
('5', 'tyres', Decimal('390.00'), datetime.date(1994, 4, 12), Decimal('100.0000000000'))

'''

You might also like