0% found this document useful (0 votes)
64 views

Program-1: Source Code

This program generates a random number between 1 and 6 to simulate rolling a dice. It asks the user if they want to roll the dice again and will continue generating random numbers and printing them out until the user enters 'N' to quit.

Uploaded by

Mukesh Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Program-1: Source Code

This program generates a random number between 1 and 6 to simulate rolling a dice. It asks the user if they want to roll the dice again and will continue generating random numbers and printing them out until the user enters 'N' to quit.

Uploaded by

Mukesh Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Program-1

This program specifically performs splitting of file data and insert “#” in the
end of every word.

SOURCE CODE My
self.txt

def word_as():

file=open(r'E:\My self.txt',"r")

lines=file.readlines()

for line in lines:

words=line.split()

for word in words:

print(word+"#",end="")

print("")

file.close()

#main program

word_as()

1
OUTPUT

2
Program-2
This program counts Total number of consonants ,vowels , Uppercases and
Lowercases.

SOURCE CODE Sample


Input.txt

def cnt():

f=open(r'E:\Sample Input.txt',"r")

cont=f.read()

print(cnt)

v=0

cons=0

l_c_l=0

u_c_l=0

for ch in cont:

if (ch.islower()):

l_c_l+=1

elif(ch.isupper()):

u_c_l+=1

ch=ch.lower()

if( ch in ['a','e','i','o','u']):

v+=1

elif (ch in ['b','c','d','f','g',

3
'h','j','k','l','m',

'n','p','q','r','s',

't','v','w','x','y','z']):

cons+=1

f.close()

print("Vowels are : ",v)

print("consonants are : ",cons)

print("Lower case letters are : ",l_c_l)

print("Upper case letters are : ",u_c_l)

#main program

cnt()

OUTPUT

4
Program-3
This program Remove all the line that contain character ‘a’ and write
another file.

SOURCE CODE My self.txt

myfile=open("E:\My self.txt","r")

line=" "

lst=[]

while line:

line=myfile.readline()

if "a" in line:

lst=line

myfile.close()

fobj=open("E:\SSd.txt","a")

fobj.write(lst)

fobj.close()

5
OUTPUT

Ssd.txt

6
Program-4
This program create binary file with name and roll number. If not found display
appropriate message.

SOURCE CODE Mukesh.dat

import pickle

import sys

dict={}

def write_in_file():

file=open("E:\\Mukesh.dat","ab") #a-append,b-binary

no=int(input("ENTER NO OF STUDENTS: "))

for i in range(no):

print("Enter details of student ", i+1)

dict["roll"]=int(input("Enter roll number: "))

dict["name"]=input("enter the name: ")

pickle.dump(dict,file) #dump-to write in student file

file.close()

def display():

#read from file and display

file=open("E:\\Mukesh.dat","rb") #r-read,b-binary

try:

7
while True:

stud=pickle.load(file) #write to the file

print(stud)

except EOFError:

pass

file.close()

def search():

file=open("E:\\Mukesh.dat","rb") #r-read,b-binary

r=int(input("enter the rollno to search: "))

found=0

try:

while True:

data=pickle.load(file) #read from file

if data["roll"]==r:

print("The rollno =",r," record found")

print(data)

found=1

break

except EOFError:

pass

if found==0:

print("The rollno =",r," record is not found")

file.close()

8
while True:

print("MENU \n 1-Write in a file \n 2-display ")

print(" 3-search\n 4-exit \n")

ch=int(input("Enter your choice = "))

if ch==1:

write_in_file()

if ch==2:

display()

if ch==3:

search()

if ch==4:

print(" Thank you ")

sys.exit()

OUTPUT

9
10
Program-5
This program create a binary file with name ,roll number and marks input marks
and update the marks.

SOURCE CODE My file.dat

import pickle

student_data={}

#writing to the file-logic

no_of_students=int(input("Enter no of students "))

file=open("D:\\stud_u.dat","ab")

for i in range(no_of_students):

student_data["RollNo"]=int(input("Enter roll no"))

student_data["Name"]=input("Enter student Name:")

student_data["Marks"]=float(input("Enter student marks:"))

pickle.dump(student_data,file)

student_data={}

file.close()

11
#reading the file logic

file=open("D:\\stud_u.dat","rb")

try:

while True:

student_data=pickle.load(file)

print(student_data)

except EOFError:

file.close()

#searching and updating(i.e. raeding amd then writing) logic

found=False

roll_no=int(input("Enter the roll no to search:"))

file=open("D:\\stud_u.dat","rb+")

try:

while True:

pos=file.tell()

student_data=pickle.load(file)

if(student_data["RollNo"]==roll_no):

#print(student_data["Name"]

student_data["Marks"]=float(input("Enter marks to be updated"))

file.seek(pos)

pickle.dump(student_data,file)

found=True

except EOFError:

if(found==False):

12
print("Roll no not found please try again")

else:

print("Student marks updated successfully.")

file.close()

#Displaying logic

file=open("D:\\stud_u.dat","rb")

try:

while True:

student_data=pickle.load(file)

print(student_data)

except EOFError:

file.close()

13
OUTPUT

14
Program-6
This program generate random number between 1 and 6 ( simulates a dice)

SOURCE CODE
import random

#Lets roll a die

Mess=input("Do you want to Start Rolling Die Y/N:")

str1=" "

while Mess=="Y":

die=random.randint(1,6)

print(die)

Mess=input("Do you want to Start Rolling Die Y/N:")

OUTPUT

15
Program-7
This program is a CSV file by entering user-Id and password , read and search the
password for given user-Id.

SOURCE CODE
import csv

def register():

with open("users.csv",mode="a", newline="")as f:

writer = csv.writer(f,delimiter=",")

email = input("Please enter mail: ")

password = input("Please enter Password: ")

password2 = input("Please Re-type Password: ")

if password == password2:

writer.writerow([email,password])

print("Registrartion is succesfull!")

else:

print("Please try again.")

def login():

email = input("Please enter email:")

password = input("Please enter your password:")

with open("users.csv",mode='r') as f:

reader = csv.reader(f,delimiter=",")

for row in reader:

if row ==[email,password]:

print("You are logged in!")

16
return True

print("Please try again!")

return False

register()

login()

OUTPUT

17

You might also like