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

Solutions

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

Solutions

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

7.

def display():
fh=open("story.txt","r")
for line in fh:
words=line.split()
newline="#".join(words)
print(newline)
fh.close()

display()

output:
Thus#the#logical#errors#are#harder#to#locate.
Python#is#a#Free#and#Open#Source#language

8. def countchars():
fh=open("story.txt","r")
txt=fh.read()
vcount=ccount=ucount=lcount=0
for ch in txt:
if ch in "AEIOUaeiou":
vcount+=1
if ch.isalpha() and ch not in "AEIOUaeiou":
ccount+=1
if ch.isupper:
ucount+=1
if ch.islower():
lcount+=1
print("No of vowels=",vcount)
print("No of consonants=",ccount)
print("No of uppercase=",ucount)
print("No of lowercase=",lcount)
fh.close()

countchars()

output:
No of vowels= 30
No of consonants= 41
No of uppercase= 89
No of lowercase= 66

9. def copylines():
fhr=open("story.txt","r")
fhw=open("newstory.txt","w")
for line in fhr:
if 'a' in line:
fhw.write(line)
fhr.close()
fhw.close()

copylines()

10. import pickle


def createfile():
fhw=open("names.dat","wb")
print("Enter 5 records:")
for i in range(5):
no=int(input("Enter number: "))
name=input("Enter name: ")
L=[no,name]
pickle.dump(L,fhw)
fhw.close()

def search(rno):
fhr=open("names.dat","rb")
found=0
while True:
try:
L=pickle.load(fhr)
if L[0]==rno:
found=1
print(L[1])
except EOFError:
break
if found==0:
print(("Value not found")
createfile()
r=int(input("Enter rollno to search"))
search(r)

output:
Enter 5 records:
Enter number: 1
Enter name: Sana
Enter number: 2
Enter name: Anne
Enter number: 3
Enter name: Hema
Enter number: 4
Enter name: Diya
Enter number: 5
Enter name: Rhea
Enter rollno to search3
Hema
12. import pickle
def createfile():
fhw=open("marks.dat","wb")
print("Enter 5 records:")
for i in range(5):
no=int(input("Enter number: "))
name=input("Enter name: ")
mark=float(input("Enter mark: "))
L=[no,name,mark]
pickle.dump(L,fhw)
fhw.close()

def modify(rno):
fhr=open("marks.dat","rb")
rows=[]
while True:
try:
L=pickle.load(fhr)
if L[0]==rno:
newmark=float(input("Enter new mark: "))
L[2]=newmark
rows.append(L)
except EOFError:
break
fhr.close()
fhw=open("marks.dat","wb")
for x in rows:
pickle.dump(x,fhw)

createfile()
r=int(input("Enter rollno to modify"))
modify(r)

import pickle
def createfile():
fh=open("marks.dat","wb")
print("Enter 5 records:")
for i in range(5):
no=int(input("Enter number: "))
name=input("Enter name: ")
mark=float(input("Enter mark: "))
L=[no,name,mark]
pickle.dump(L,fh)
fh.close()

def display():
fh=open("marks.dat","rb")
while True:
try:
L=pickle.load(fh)
if L[2]>95:
print(L)
except EOFError:
break
fh.close()

createfile()
display()
Output:
Enter 5 records:
Enter number: 1201
Enter name: aaa
Enter mark: 98
Enter number: 1202
Enter name: bbb
Enter mark: 94
Enter number: 1203
Enter name: ccc
Enter mark: 89
Enter number: 1204
Enter name: ddd
Enter mark: 99
Enter number: 1205
Enter name: eee
Enter mark: 78
[1201, 'aaa', 98.0]
[1204, 'ddd', 99.0]

13. import random


def randomdice():
print("Starting to roll...")
while True:
n=random.randint(1,6)
print("You got",n)
resp=input("Roll again? y/n ")
if resp not in"Yy":
break
print("Thank you for playing")

randomdice()

Output:
Starting to roll...
You got 6
Roll again? y/n y
You got 3
Roll again? y/n y
You got 4
Roll again? y/n y
You got 5
Roll again? y/n y
You got 1
Roll again? y/n n
Thank you for playing

14. Stack=[]
def PUSH(x):
Stack.append(x)

def POP():
if len(Stack)==0:
print("Stack empty..")
return None
else:
return Stack.pop()

while True:
choice=int(input("Enter 1 to push, 2 to pop, 0 to exit:"))
if choice==1:
item=eval(input("Enter value to push:"))
PUSH(item)
elif choice==2:
item=POP()
print("value popped:",item)
else:
break
print("Current stack status:",Stack)

output:
Enter 1 to push, 2 to pop, 0 to exit:1
Enter value to push:10
Current stack status: [10]
Enter 1 to push, 2 to pop, 0 to exit:1
Enter value to push:20
Current stack status: [10, 20]
Enter 1 to push, 2 to pop, 0 to exit:1
Enter value to push:30
Current stack status: [10, 20, 30]
Enter 1 to push, 2 to pop, 0 to exit:2
value popped: 30
Current stack status: [10, 20]
Enter 1 to push, 2 to pop, 0 to exit:2
value popped: 20
Current stack status: [10]
Enter 1 to push, 2 to pop, 0 to exit:2
value popped: 10
Current stack status: []
Enter 1 to push, 2 to pop, 0 to exit:2
Stack empty..
value popped: None
Current stack status: []
Enter 1 to push, 2 to pop, 0 to exit:0
15. import csv
def createfile():
fhw=open("passwords.csv","w",newline="")
wobj=csv.writer(fhw)
print("Enter 5 rows:")
for i in range(5):
uid=input("Enter userid: ")
pwd=input("Enter password: ")
L=[uid,pwd]
wobj.writerow(L)
fhw.close()

def search(uid):
fhr=open("passwords.csv","r")
robj=csv.reader(fhr)
found=0
for x in robj:
if x[0]==uid:
found=1
print(x[1])
fhr.close()
if found==0:
print("Value not found")
createfile()
id=input("Enter id to search: ")
search(id)

output:
Enter 5 rows:
Enter userid: aaa
Enter password: 01ab
Enter userid: bbb
Enter password: 12bc
Enter userid: ccc
Enter password: 23cd
Enter userid: ddd
Enter password: 34de
Enter userid: eee
Enter password: 45ef
Enter id to search: ddd
34de

You might also like