0% found this document useful (0 votes)
21 views5 pages

CS Practical 6-10

The document describes 10 programs with different functions: 1. A program to create a dictionary of student roll numbers and names and search for a given roll number. 2. A random number generator that simulates rolling a dice between 1-6. 3. A program to read a text file line by line and print each word separated by a #. 4. A program to remove all lines containing "a" from a file and write to a new file. 5. A program to create a text file and print lines starting with T, t, P, or p.

Uploaded by

ankushrio83006
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)
21 views5 pages

CS Practical 6-10

The document describes 10 programs with different functions: 1. A program to create a dictionary of student roll numbers and names and search for a given roll number. 2. A random number generator that simulates rolling a dice between 1-6. 3. A program to read a text file line by line and print each word separated by a #. 4. A program to remove all lines containing "a" from a file and write to a new file. 5. A program to create a text file and print lines starting with T, t, P, or p.

Uploaded by

ankushrio83006
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/ 5

S.

No: 6
AIM : Write a program using function to create a dictionary with roll number
and name of 5 students. Search for a given roll no. And display the name, if not
found, display appropriate message.

SOURCE CODE:
def search(d):
keys = d.keys()
while True:
q = input("Do you want to search for a key? Y/N:")
if q == 'Y' or q == 'y':
k = int(input("Enter the Key: "))
if k in keys:
print("The roll number", k,"exists in dictionary")
print("Name of Student is" , d[k])
else:
print("Roll no. not found")
else:
break
d = {}
for i in range(5):
name = input("Enter name: ")
roll = int(input("Enter roll. no: "))
d[roll] = name
print(d)
search(d)

Output:
Enter name: Hari
Enter roll. no: 69
Enter name: Binod
Enter roll. no: 48
Enter name: Christine
Enter roll. no: 63
Enter name: Donna
Enter roll. no: 23
Enter name: Esme
Enter roll. no: 14
{69: 'Hari', 48: 'Binod', 63: 'Christine', 23: 'Donna', 14: 'Esme'}
Do you want to search for a key? Y/N: Y
Enter the Key: 48
The roll number 48 exists in dictionary
Name of Student is Binod
Do you want to search for a key? Y/N: Y
Enter the Key: 3
Roll no. not found
Do you want to search for a key? Y/N: N

S. No: 7
AIM : Write a random number generator that generates random no. Between
1 and 6 (estimate a dice).

SOURCE CODE:
import random
def gen():
x = 'y'
while x == "Y" or x == "y":
n = random.randint(1,6)
print(n)
if n==6:
print("You win!")
else:
print("You lose!")
x= input("press Y to roll again")
gen()

Output:
6
You win!
Press y to roll again: y
2
You lose!
Press y to roll again: y
4
You lose!
Press y to roll again: y
6
You win!
Press y to roll again: y
3
You lose!
Press y to roll again: n

S. No: 8
AIM : Write a program to read a text file line by line and display each word
separated by a #.

SOURCE CODE:
file = open('Testrun.txt',"r")
z = file.readlines()
for i in z:
wor = i.split()
print("#".join(wor))
file.close

Output:
Testrun.txt :
Hey there!
I just learnt how to connect text files to python programs.
It's fun and easy.
Hope you take out the time to do so as well.

Sample Output:
I#just#learnt#how#to#connect#text#files#to#python#programs.
It's#fun#and#easy.
Hope#you#take#out#the#time#to#do#so#as#well.

S. No: 9
AIM : Write a Program to remove all the lines that contains the character ‘a’
in a file and write it to another file.
SOURCE CODE:
myfile = open("file.txt",'r')
lst1 = []
line1 = myfile.readlines()
for i in line1:
if 'a' not in i:
lst1 += i
d = open("file1.txt",'w')
d.writelines(lst1)
d.close()

Sample Output:
File.txt:
Hey there!
I just learnt how to connect text files to python programs.
It's fun and easy.
Hope you
take out the time to do
so as well.
File1.txt:
Hey there!
Hope you

S. No: 10
AIM : Write a program to create a text file and print the lines starting with ‘T’
or ‘P’ (Both uppercase and lowercase)

SOURCE CODE:
myfile = open(r 'C:\Users\Desktop\file.txt','w+')
myfile.write("Takes two to tango \nKill two birds with one stone \nPower of a
man.")
myfile.seek(0)
lines = myfile.readlines()
for i in lines:
if i[0]=='T' or i[0]=='t' or i[0]=='P' or i[0] == 'p':
print(i)
myfile.close()

Output:
File.txt:
Takes two to tango.
Kill two birds with one stone
Power of a man

Sample Output:
Takes two to tango.
Power of a man

You might also like