CSC Record Xii prg5-13
CSC Record Xii prg5-13
QUES: Write a program to play a dice game that generates 3 random numbers between 1 and
6. If all the three random numbers are equal, the score is increased by 50 points and the
program quits. If any two random numbers are equal, then score is increased by 25 points
otherwise, score is increased by 10 points.
AIM: To write a python program to play a dice game that generates 3 random numbers
between 1 and 6 and give score as per given instructions.
CODE:
import random
score=0
while True:
a=random.randint(1,6)
b=random.randint(1,6)
c=random.randint(1,6)
print("a=",a,"b=",b,"c=",c)
if a==b==c:
score+=50
print("adding 50...")
break
elif a==b or b==c or a==c :
score+=25
print("adding 25...")
else:
score+=10
print("adding 10...")
print("The score is ",score)
ans=input("Continue(y/n)?")
if ans=='n' or ans=='N':
break
print("The Final score is ",score)
QUES: Write a program to read a text file line by line and display each word separated by a #.
AIM: To write a python program to to read a text file line by line and display each word
separated by a #.
CODE:
def Display():
f=open(r"d:/test.txt","r")
lines=f.readlines()
for i in lines:
word=i.split()
for i in word:
print(i,end='#')
print()
f.close()
while True:
print("1.Display word with #\n2. Exit")
ch=int(input("Enter a choice"))
if ch==1:
Display()
elif ch==2:
break
else:
print("Invalid input....Try again...")
def LowUpp():
f=open("D:\\test.txt","r")
d=f.read()
lc=uc=0
for i in d:
if i.isalpha():
if i.islower():
lc+=1
else:
uc+=1
print("Number of lowercase characters are ",lc)
print("Number of uppercase characters are ",uc)
f.close()
while True:
print(''1. No. of vowels and consonants\n2. No. of lower and upper case\n3. No. of the
word\n4. Exit'')
ch=int(input("Enter your choice:"))
if ch==1:
VowCons()
elif ch==2:
LowUpp()
elif ch==3:
break
else:
print("Invalid entry....Try again....")
XII CS RECORD WORK Page 15
PROGRAM : 8 DATE : __________________
QUES: Write a python program to get roll number, names and marks of 3 students of a class
(get from user) and store the details in a file named ‘marks.txt’
AIM: To get roll number, names and marks of 3 students of a class (get from user) and store
the details in a file named ‘marks.txt’
CODE:
def Write():
f=open(r"d:/marks.txt","w")
for i in range(3):
rno=input("enter roll no : ")
name=input("enter name : ")
marks=input("enter marks : ")
f.write(rno+','+name+','+marks+'\n')
f.close()
def Display():
f=open(r"d:/marks.txt","r")
print(f.read())
while True:
print("1.Write\n2. Display\n3.Exit")
ch=int(input("Enter a choice"))
if ch==1:
Write()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid input....Try again...")
QUES: Write a python program to add two more student records with roll number, names and
marks into the file named ‘marks.txt’
AIM: To add two more student records with roll number, names and marks into the file named
‘marks.txt’
CODE:
def Append():
f=open(r"d:/marks.txt","a")
for i in range(2):
rno=input("enter roll no : ")
name=input("enter name : ")
marks=input("enter marks : ")
f.write(rno+','+name+','+marks+'\n')
f.close()
def Display():
f=open(r"d:/marks.txt","r")
print(f.read())
while True:
print("1.Append\n2. Display\n3.Exit")
ch=int(input("Enter a choice"))
if ch==1:
Append()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid input....Try again...")
AIM: To get admission number, name and total marks of 3 students from user and store the
details in a binary file named ‘stud.dat’. Also to read and display the file.
CODE:
import pickle
def Add():
d={}
f=open("stud.dat","wb")
for i in range(3):
admno=int(input("Enter Admission no:"))
name=input("Enter student name:")
tot_marks=int(input("Enter Total marks obtained:"))
d['Admno']=admno
d['Name']=name
d['Tot Marks']=tot_marks
pickle.dump(d,f)
print("Record",i+1," Added Successfully.")
f.close()
def Display():
f=open("stud.dat","rb")
while True:
try:
d=pickle.load(f)
print(d)
except EOFError:
break
f.close()
while True:
print("1. Add Record\n2. Display Record\n3. Exit")
ch=int(input("Enter your choice:"))
if ch==1:
Add()
elif ch==2:
Display()
elif ch==3:
break
else:
print("Invalid entry....Try again....")
QUES: Write a python program to search a binary file “stud.dat” and display the records
whose total marks is greater than 400.
AIM: To search a binary file “stud.dat” and display the records whose total marks is greater
than 400.
CODE:
import pickle
def Search():
f=open("stud.dat","rb")
found=False
while True:
try:
d=pickle.load(f)
if d['Total Marks']>400:
print(d)
found=True
except EOFError:
break
if found==False:
print("Record not found...")
f.close()
while True:
print("1. Search\n2.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
Search()
elif ch==2:
break
else:
print("Invalid entry....Try again....")
QUES: Write a python program to get item number, name of item and price of any 3 items
from a departmental store and store the details in a csv file named ‘item.dat’. Also read and
display the file.
AIM: To write a python program to get item number, name of item and price of any 3 items
from a departmental store and store the details in a csv file named ‘item.dat’. Also to read and
display the file.
CODE:
import csv
def Write():
f=open("item.csv","w",newline='\n')
cwriter=csv.writer(f)
cwriter.writerow([‘itemno’,'itemname','price'])
for i in range(3):
print("Item Record # ",i+1)
itemno=int(input("Enter item no : "))
itemname=input("Enter name of item : ")
price=int(input("Enter its price : "))
cwriter.writerow([itemno, itemname, price])
f.close()
def Read():
f=open("stud.csv","r")
creader=csv.reader(f)
for i in creader:
print(i)
f.close()
while True:
print("1. Write\n2. Read\n3. Exit")
ch=int(input("Enter a choice"))
if ch==1:
Write()
elif ch==2:
Read()
elif ch==3:
break
else:
print("Invalid input....Try again...")
AIM: To create a CSV file by entering user-id and password, read and search the password for
given userid.
CODE:
import csv
def Write():
f=open("userpasswd.csv","w",newline='\n')
cwriter=csv.writer(f)
cwriter.writerow(['userid','password'])
for i in range(3):
print("user and password # ",i+1)
userid=input("Enter userid : ")
password=input("Enter its password : ")
cwriter.writerow([userid,password])
f.close()
def Search():
f=open("userpasswd.csv","r")
creader=csv.reader(f)
uid=input("Enter userid whose password is to be searched")
found=False
f.seek(0)
for i in creader:
if i[0]==uid:
print("record found")
print(i)
found=True
if found==False:
print("Record not found...")
f.close()
while True:
print("1. Write")
print("2. Search")
print("3. Exit")
ch=int(input("Enter a choice"))
if ch==1:
Write()
elif ch==2:
Search()
elif ch==3:
break
else:
print("Invalid input....Try again...")