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

Computer Science Project File

This document contains 15 programming problems involving writing functions and programs in Python. Some examples include writing functions to calculate factorials and Fibonacci sequences, counting lines in a file, replacing spaces with hyphens in a text file, and basic mathematical operations in a calculator program using functions. The problems cover concepts like file handling, strings, lists, dictionaries, random number generation, and working with CSV files.

Uploaded by

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

Computer Science Project File

This document contains 15 programming problems involving writing functions and programs in Python. Some examples include writing functions to calculate factorials and Fibonacci sequences, counting lines in a file, replacing spaces with hyphens in a text file, and basic mathematical operations in a calculator program using functions. The problems cover concepts like file handling, strings, lists, dictionaries, random number generation, and working with CSV files.

Uploaded by

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

COMPUTER SCIENCE

PROJECT FILE

Garvi Patel
XII B
5
1. Write a program which include following user defined
functions. 1) to print factorial of given no 2)to print fibonacei
series for no .
def fact(x):
z=1
for i in range(1,x+1):
z*=i
print(f 'factorial of {x} is {z}')
def fib(n):
l=[0,1]
for i in range(n-2):
l1 = l[-1] + l[-2]
l.append(l1)
print('fibonacci series is',l)
x=int(input('Enter no. for factorial:'))
n=int(input('Enter no. of terms in fibonacci series:'))
fact(x)
fib(n)
2. Write a program to count the total no of lines and
count the total no of lines starting from a,b,c from the
file.
f = open('//Users//macbookpro//Documents//
test1.applescript', 'r')
r = f.readlines()
c=0
for i in r:
if i[0] == 'A' or i[0] == 'B' or i[0] == 'C':
c += 1
print(c)
f.close()
3. Write a profram to replace all the spaces from text
with '-' from the file.
def sp_hyphen(r):
i = r.replace(' ', '-')
print(i)
f = open('//Users//macbookpro//Documents//
test1.applescript', 'r')
r = f.read()
sp_hyphen(r)
f.close()
4. Write a progam to check if a no is divisible by 2 or 3
using nested if.
def div2_3(x):
if x>0 or x<0:
if x%2 == 0:
if x%3 == 0:
print("Divisible by 3 and 2")
else:
print("divisible by 2 not divisible by 3")
else:
if x%3 == 0:
print("divisible by 3 not divisible by 2")
else:
print("Not divisible by 2 and 3")
else:
print('Enter a valid number')
x=int(input('Enter a no. to check divisibility by 2 or 3:'))
div2_3(x)
5. Make a simple calculator using user defined
functions.
def add(x,y):
z=x+y
return z
def sub(x,y):
z=x-y
return z
def mul(x,y):
z=x*y
return z
def div(x,y):
z=x/y
return z
print('1. Addition\n2. Subtraction\n3. Multiply\n4.
Division')
ans='y'
while ans=='y':
x = int(input('Enter first iber:'))
y = int(input('Enter second iber:'))
ch = input('Enter your choice:')
if ch=='1':
z=add(x,y)
print(z)
if ch=='2':
z=sub(x,y)
print(z)
if ch=='3':
z=mul(x,y)
print(z)
if ch=='4':
z=div(x,y)
print(z)
ans=input('Do you want to continue(y or n):')
else:
print('Thank you')
6.Write a program to display all the angstrom
numbers from 1 to 200.
low=1
up=200
for i in range(low,up + 1):
sum = 0
num = i
while num > 0:
dig = num % 10
sum += dig ** 3
num //= 10
if i == sum:
print(i)
7. Write a program to count the occurrence of a
specific chachater in a string.
def occ(st,ch):
c=0
for i in st:
if i == ch:
c += 1
print(f'{ch} occurs {c} times in {st}')
st=input('Enter a string:')
ch=input('Enter a character to count occurences:')
occ(st,ch)
8.Create a binary file using name and roll no and . Search
for a given roll no and display a specific message.
import pickle
f=open('//Users//macbookpro//Documents// test2.applescript', 'wb')
d={}
for i in range(5):
rno = input('Enter roll no:')
nm = input('Enter name:')
d[rno]=nm
pickle.dump(d,f)
f.close()
print('file has been created with given roll numbers and names in
dictionary form')
9.Write a random no generator that generates
random no's between 1 and 6.
import random as r
print(r.randint(1,6))
10.Create a csv file using a user id and password
using given user id.
import csv
f=open('//Users//macbookpro//Documents// test3.applescript', 'w')
w=csv.writer(f)
l1=[]
for i in range(5):
id=input('Enter id:')
pas=input('Enter password:')
l1.append(id)
l1.append(pas)
w.writerow(l1)
l1.clear()
f=open('//Users//macbookpro//
Documents// test3.applescript', 'r')
r=csv.reader(f)
ch=input('Enter id for password:')
for i in r:
if i[0]==ch:
p=i[1]
print('passwrod for id:',ch,'is',p)
11. Write a program to accept a string and print
the no of appearance in given string using user
defined function.
def ev(x):
if x%2==0:
print('No. is even')
else:
print('No. is odd')
x=int(input('Enter a number:'))
ev(x)
12. Write a program to create the mirror of the
given string using a function.
def mir(x):
st=x[::-1]
print(st)
x=input('Enter a string:')
mir(x)
13.Write a program to generate all the values from 1
to 10 and then removing all the odd no's from the list
import random as r
l=[]
for i in range(10):
l.append(r.randint(1,10))
print('list before removing odd numbers is',l)
for i in l:
if i%2!=0:
l.remove(i)
print('list after removing odd numbers is',l)
14.Write a program to accept a string and print no of
uppercase, lowercase, vowels ,consonants and
spaces.
def lo_up_sp(st):
global lc,uc,sc
lc=uc=sc=0
for i in st:
if i.islower():
lc += 1
elif i.isupper():
uc += 1
elif i.isspace():
sc += 1
def vo_con(st):
global vc,cc
vc=cc=0
for i in st:
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
vc += 1
else:
cc += 1
st=input('Enter a string:')
lo_up_sp(st)
vo_con(st)
cc-=sc
print('lc',lc,
'uc',uc,
'vc',vc,
'cc',cc,
'sc',sc)
15.Wap to get players name and their score. Input in a name
using python. read the csv file to display name and score, if
player name is not found display an appropriate message

import csv
f=open('//Users//macbookpro//Documents// test4.applescript','w')
w=csv.writer(f)
l=[]
for i in range(5):
nm=input("Enter player's name:")
sc=input('Enter score:')
l.append(nm)
l.append(sc)
w.writerow(l)
l.clear()
f=open('//Users//macbookpro//Documents//
test4.applescript', 'r')
r=csv.reader(f)
ch=input("Enter player's name to get score:")
for i in r:
if i[0]==ch:
s=i[1]
print('Score for player',ch,'is',s)
else:
print('The player name does not exist in
the records')

You might also like