Program:16
Create a binary file with roll number, name and marks. Input a roll number and update the marks.
import pickle import os fh=open("student.dat","wb") lst=[] try: while True: r=int(input('enter roll no.: ')) n=input('enter name: ') m=float(input('enter marks: ')) lst=[r,n,m] pickle.dump(lst,fh) ch=input('do you want to cont') if ch=='Y' or ch=='y': continue else: break except EOFError: fh.close() fh=open("student.dat","rb") t=open("temp.dat","wb") r=int(input('enter the roll number to search')) lst1=[] z=0 try: while True: lst1=pickle.load(fh) if lst1[0]==r: lst1[2]=float(input('enter new marks: ')) pickle.dump(lst1,t) z=1 else: pickle.dump(lst1,t) except EOFError: if z==0: print('record not found') else: print('marks updated successfully') fh.close() t.close() os.remove("student.dat") os.rename("temp.dat","student.dat") fh=open("student.dat","rb") lst1=[] try: while True: lst1=pickle.load(fh) print(lst1) except EOFError: fh.close()
Program:17
Write a random number generator that generates random numbers between 1 and 6 (smulates a dice).
#random number generator import random while True: r=random.randint(1,6) print (r) ch=input('want to continue: ') if ch=='Y' or ch=='y': continue else: break
Program:18
Create a CSV file by entering user-id and password, read and search the password for given user- id.
import csv f=open("details.csv","w",newline='') w=csv.writer(f) for i in range(3): u=input('enter user ID: ') p=input('enter password: ') l=[u,p] w.writerow(l) f.close() f=open("details.csv","r",newline='') r=csv.reader(f) u1=input('enter user id: ') for i in r: if i[0]==u1: print(i[1])