0% found this document useful (0 votes)
12 views9 pages

Untitled Document

Uploaded by

sanjithas
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)
12 views9 pages

Untitled Document

Uploaded by

sanjithas
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/ 9

'''Write a function nthRoot() that receives two parameters: x, n and

returns nth root of x, i.e: x1/n.


#Write the main program using this function'''

def nthRoot(num,n):
nth=num**(1/n)
print("nth root=",nth)
N=int(input("Enter a number:"))
N1=int(input("Enter another number:"))
nthRoot(N,N1)
''Write a function that takes a number n and then returns a randomly
generated number having exactly n digits.
Write the main program for this function'''
import random
def ran(x):
r=10**(x-1)
r2=(10**x)-1
r3=random.randint(r,r2)
return r3
n=int(input("Enter a number: "))
print("The random number is: ", ran(n))
'''Write a function that accepts principal, rate and time and
calculates the compound interest.
Write the main program for this function'''
def calculate (p, r, t) :
Amount = p * (pow((1 + r / 100), t))
CI = Amount - p
print("Compound interest is", CI)
p = float(input( "Enter the principal amount : " ) )
r = float(input( "Enter the rate of interest : " ) )
t = float(input( "Enter total time : " ) )
calculate (p, r, t )
'''Write a menu driven program to create a calculator using user
defined functions for various operations.
Options should include:
1. Compute
2. Recall the previously calculated operations (History)
3. Quit'''

def addition(x,y):
global s
s=x+y
return s
def subtraction(x,y):
global s1
s1=x-y
return s1
def multiplication(x,y):
global s2
s2=x*y
return s2
def division (x,y):
global s3
s3=x/y
return s3
print('Enter 1 for compute \nEnter 2 for History \nEnter 3 to quit')
i=1
while i == True:
ch=int(input('Enter your choice: '))
if ch==1: #choice 1 (compute)
global c
global a
global b
c=input('Enter the operation: ')
a=int(input('Enter the number: '))
b=int(input('Enter the number: '))
if c=='+':
print(addition(a,b))
elif c=='-':
print(subtraction(a,b))
elif c=='*':
print(multiplication(a,b))
elif c=='/':
print(division(a,b))
else:
print('invalid')
elif ch==2: #choice 2 (history)
print('HISTORY :')
if c=='+':
print(a,'+',b,'=',s)
elif c=='-':
print(a,'-',b,'=',s1)
elif c=='*':
print(a,'*',b,'=',s2)
elif c=='/':
print(a,'/',b,'=',s3)
elif ch==3: #choice 3 (quit)
print('Exiting..')
break
else: # else
print('invalid')
5
def replaceVowel(st):
#Create an empty string
newstr = ''
for character in st:
#Check if next character is a vowel
if character in 'aeiouAEIOU':
#Replace vowel with *
newstr += '*'
else:
newstr += character
return newstr
#End of function
st = input("Enter a String: ")
st1 = replaceVowel(st)
print("The original String is:", st)
print("The modified String is:", st1)

'''Write a program using a function Prime that accepts a number as


argument and checks if the number is prime'''
def prime(n):
for i in range(2,n):
if n%i==0:
print("Not a prime number")
break
else:
print("Prime number")
break
n=int(input("Enter a number: "))
prime(n)
'''Write a function to accept a list and find the sum of elements in
the list.
Write the main program and invoke this function'''
def sum_list(items):
sum_numbers=0
for x in items:
sum_numbers+=x
return sum_numbers
n=int(input("Enter the number of elements you want in the list: "))
l=[]
for i in range(n):
n1=int(input("Enter an element: "))
l.append(n1)
print("The sum of all the elements in the list is: ",sum_list(l))
8
f=open("File1.txt","w")
L=[]
L1=[]
x=True
while x:
a=int(input("Enter 1 to input a new value or enter 0 to exit:
"))
if a==1:
str1=input("Enter a string: ")
str1=str1+"\n"
L.append(str1)
else:
x=False
f.writelines(L)
f.close()
f=open("File1.txt","r")
lines=f.readlines()
for i in lines:
if "Red" in i:
x=i.replace("Red","White")
L1.append(x)
else:
L1.append(i)
L1.pop(2)
f.close()
f=open("File1.txt","w")

f.seek(0)
f.writelines(L1)
f.close()
f=open("File1.txt",'r')
data =f.readlines()
for b in data:
print(b)
f.close()
9
import os
f=open("E:\\simple.txt","r")
fh=open("E:\\sample.txt","w")
lines=f.readlines()
new=[]
old=[]
for i in lines:
if "a" in i:
old.append(i)
else:
new.append(i)
fh.writelines(new)
fh.close()
fh=open("E:\\sample.txt","r")
l=fh.readlines()
for i in l:
print(i)
fh.close()
f.close()
os.remove("E:\\simple.txt")
os.rename("E:\\sample.txt", "E:\\simple.txt")
10
f=open("E:\\Input values.txt",'w')
x=True
L=[]
while x:

a=int(input("Enter 1 to input a new value or enter 0 to exit:


"))
if a==1:
str1=input("Enter the sting: ")
str1=str1+"\n"
L.append(str1)
else:
x=False
f.writelines(L)
f.close()
upper=open("E:\\Uppervalues.txt",'w')
lower=open("E:\\Lowervalues.txt",'w')
other=open("E:\\Othervalues.txt",'w')
up1=[]
low1=[]
ot=[]
f=open("E:\\Input values.txt",'r')
lines=f.readlines()
up="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
low="abcdefghijklmnopqrstuvwxyz"
for i in lines:
for j in i:
if j in up:
up1.append(j)
elif j in low:
low1.append(j)
else:
ot.append(j)
upper.writelines(up1)
lower.writelines(low1)
other.writelines(ot)
upper.close()
lower.close()
other.close()
f.close()
11
file = open("E:\\Rhyme.txt","w")
file.writelines(["the itsy bitsy spider climbed up the water spout\n",
"down came the rain and washed the spider out \n",
"out came the sun, and dried up all the rain\n",
"and the itsy bitsy spider went up the spout again \n"])
file.close()
file = open("E:\\Rhyme.txt","r")
file1 = open("Rhymeindex.txt","w")
n = 1
d = {}
lines = file.readlines()
print(lines)
for li in lines:
words = li.upper().split()
print(words)
for w in words:
print(w)
if w not in d:
d.update({w:[n]})
elif n not in d[w]:
d[w].append(n)
n = n + 1
for i in d:
fin = i + ":" + str(d[i]).strip('[]') + '\n'
file1.write(fin)
file1.close()
file.close()
file = open ("Rhymeindex.txt","r")
for i in file:
print(i)
file.close()
12
import pickle
f=open('student1212.dat',"wb")
print('enter 1 for data entry ')
print('enter 0 for exiting')
d=[]
i = 1
while True:
ch=int(input("enter your choice"))
if ch==1:
name=input('enter the name')
roll=int(input('enter the roll number'))
d=[name,roll]
pickle.dump(d,f)
elif ch==0:
print('exiting...')
f.close()
break
else:
print('invalid')

f2=open('student1212.dat',"rb")
flag=False
d1=[]
i=1
k=int(input('enter roll number:'))
while True:
try:

d1=pickle.load(f2)
if d1[1] == k :
print(d1)

flag = True
except EOFError:
f2.close()
break
if flag == True:
print('RECORD FOUND')
else:
print('NO RECORD FOUND')
13
import pickle
f=open('hospital.dat',"wb")
l=[]
while True:
ch=int(input('enter your choice'))
if ch==1:
pn=input('enter the patient name')
rn=int(input('enter the room number'))
bt=input('enter the blood type')
l=[pn,rn,bt]
pickle.dump(l,f)
elif ch==0:
print('exiting......')
break
else:
print('Invalid')
f.close()

f2=open('hospital.dat',"r+b")
r=int(input('enter the room number'))
pnn=input('enter the patient name')
pbt=input('enter the blood type')
f=False
while True:
try:
l1=pickle.load(f2)
t=f2.tell()
if l1[1]==r:
l1[0]=pnn
l1[2]=pbt
f2.seek(t,0)
pickle.dump(l1,f2)
f=True

print(l1)
except EOFError:
break
f2.close()

if f==True:
print('RECORD FOUND')
else:
print('RECORD NOT FOUND ')
f3=open('hospital.dat',"rb")
while True:
try:
l5=pickle.load(f3)
print(l5)
except EOFError:
f3.close()
break
14
import csv
f=open("studentmarks.csv","w")
obj=csv.writer(f)
print('enter 1 for data entry')
print('enter 0 for exiting..')
obj.writerow(['student name','english marks','math marks','science
marks','gender']
)
while True:
ch=int(input('enter'))
if ch==1:
sn=input('enter the name')
em=int(input('enter english marks'))
mm=int(input('enter math marks'))
sm=int(input('enter science marks'))
g=input('enter the gender')

obj.writerow([sn,em,mm,sm,g])
elif ch==0:
f.close()
print('exiting..')
f=open("studentmarks.csv","r")
f2=csv.reader(f)
for i in f2:
for j in i:
if j=='female':
print('NAME : ',i[0])
print('ENGLISH MARKS : ',i[1])
print('MATH MARKS : ',i[2])
print('SCIENCE MARKS : ',i[2])
break
else:
print('invalid')
15
import csv
file=open("student.csv",'w',newline='')
def writecsv():
writerobj=csv.writer(file)
writerobj.writerow(['userid','password'])
ans='y'
while ans=='y':
uid=input("enter user id")
pwd=input("enter password")
writerobj.writerow([uid,pwd])
ans=input("want to add more y/n")
print("Data saved")
file.close()
def search():
file=open("student.csv",'r')
uid1=input("Enter the id to search")
readcv=csv.reader(file)
for i in readcv:
if(i[0]==uid1):
print("password" , i[1])

writecsv()
search()

You might also like