0% found this document useful (0 votes)
9 views81 pages

BCA Python Lab Manual for Beginners

The document is a complete lab manual for the BCA first-year students at the University of Madras, focusing on problem-solving using Python. It includes various programming exercises such as temperature conversion, pattern construction, student mark list calculation, and more, along with algorithms and sample codes. Each exercise is structured with aims, algorithms, programs, flowcharts, outputs, and results, demonstrating the successful execution of Python programs.

Uploaded by

Nagalakshmi R
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)
9 views81 pages

BCA Python Lab Manual for Beginners

The document is a complete lab manual for the BCA first-year students at the University of Madras, focusing on problem-solving using Python. It includes various programming exercises such as temperature conversion, pattern construction, student mark list calculation, and more, along with algorithms and sample codes. Each exercise is structured with aims, algorithms, programs, flowcharts, outputs, and results, demonstrating the successful execution of Python programs.

Uploaded by

Nagalakshmi R
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

lOMoARcPSD|15090685

BCA I year python complete lab manual

BCA (University of Madras)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])
lOMoARcPSD|15090685

PRINCE SHRI BALAJI ARTS AND


SCIENCE COLLEGE

PONMAR, CHENNAI -600127.

(AFFILIATED TO THE UNIVERSITY OF MADRAS)

DEPARTMENT OF COMPUTER APPLICATION

PROBLEM SOLVING USING PYTHON LAB


ACADEMIC YEAR : 2023-2024

NAME :
CLASS:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PRINCE SHRI BALAJI ARTS AND


SCIENCE COLLEGE
PONMAR, CHENNAI -600127.

(AFFILIATED TO THE UNIVERSITY OF MADRAS)

REGISTER NUMBER:

This is to certify that

Studying in I BCA (Computer Application) in this college has successfully


completed his/her “PROBLEM SOLVING USING PYTHON LAB” record
for the Semester –I during the academic year 2023 – 2024.

Faculty In-charge Head of the Department

Submitted for the practical examination held on


at Prince Shri Balaji Arts and Science College.

EXTERNAL EXAMINERS:
1.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

INDEX
PROBLEM SOLVING USING PYTHON LAB

[Link] DATE TITLE [Link] [Link]


1 TEMPERATURE CONVERSION

2 CONSTRUCT PATTERN USING


NESTED LOOP

3 STUDENT MARK LIST


CALCULATION

4 AREA OF DIFFERENT SHAPES

5 PRIME NUMBERS LESS THAN 20

6 FACTORIAL USING RECURSION

7 ODD NUMBER AND EVEN


NUMBER COUNT CALCULATION

8 REVERSE A STRING WORD BY


WORD
9 COUNT OCCURENCES OF EACH
LIST ELEMENT IN THE TUPLE
10 INCREASE THE BALANCE IN
SAVINGS ACCOUNT THROUGH
RATE OF INTEREST

11 COPY THE FILE CONTENTS

12 TURTLE GRAPHICS WINDOW


CREATION

13 DRAW A SHAPES USING TURTLE

14 MENU DRIVEN USING


DICTIONARY
15 HANGMAN GAME

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 1TE TEMPERATURE CONVERSION

Date:

AIM:

To write python program to convert the given temperature from fahrenheit to celsius
and vice versa depending upon user’s choice.

ALGORITHM:
Step 1: Start
Step 2: Declare the variable ‘temp’ and read the input from user
Step 3: if a == ‘F’:
calculate con_temp=(temp-32)*5/9
else:
calculate con_temp=(9/5*temp)+32
Step 4: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

print("Enter (F) to convert Fahrenheit to Celsius")


print("Enter (C) to convert Celsius to Fahrenheit")
a = input("Enter your choice: ")
temp=int(input("Enter Temperature to convert: "))
if a=='F':
con_temp=(temp-32)*5/9
print(temp,'Degrees fahrenheit equals',con_temp,'Degree Celsius')
else:
con_temp=(9/5*temp)+32
print(temp,'Degrees Celsius equals',con_temp,'Degree fahrenheit')

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the program for the temperature conversion has been successfully executed.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 2 CONSTRUCT PATTERN USING NESTED LOOP

Date:

AIM:

To write a python program to construct the pattern using a nested loop.

ALGORITHM:

Step 1: Start

Step 2: Read the input values

Step 3: To print the pattern using the nested loop or for loop like

for i in range(num)

Step 4: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

rows = 4
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end="")
k=k-1
for j in range(0, i + 1):
print("* ", end="")
print("")

k = rows - 2

for i in range(rows, -1, -1):


for j in range(k, 0, -1):
print(end="")
k=k+1
for j in range(0, i + 1):
print("* ", end="")
print("")

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the python program to construct the pattern using the nested loop has been
executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 3TE STUDENT MARK LIST CALCULATION

Date:

AIM:

To write a program to calculate total marks,percentage and grade of a student using


python.

ALGORITHM:

Step 1: Start

Step 2: Read the input from the user (mark 1,mark 2, mark 3, mark 4, mark 5)

Step 3: Calculate sum and average

Step 4: Display sum,average and grade

Step 5: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

Mark1=int(input("Enter marks of the English subject: "))


Mark2=int(input("Enter marks of the Tamil subject: "))
Mark3=int(input("Enter marks of the Maths subject: "))
Mark4=int(input("Enter marks of the Python subject: "))
Mark5=int(input("Enter marks of the Practical subject: "))
sum=Mark1+Mark2+Mark3+Mark4+Mark5;
print("Total Marks = ",sum)
average = sum/5;
if(average>=80):
print("Your Grade is A");
elif(average>=70 and average<80):
print("Your Grade is B");
elif(average>=60 and average<70):
print("Your Grade is C");
elif(average>=40 and average<60):
print("Your Grade is D");
elif(average<40):
print("Your Grade is E");
else:
print("No Grade..!!");

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:
Thus the program to calculate the student marklist has been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 4 AREA OF DIFFERENT SHAPES

Date:

AIM:

To write a python program to find the area of triangle,square,circle and rectangle.

ALGORITHM:

Step 1: Start

Step 2: Read the input from user for side, length, breadth, radius, height.

Step 3: To calculate and display the area of rectangle, square, circle and triangle, call the

corresponding function.

Step 4: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

def square(a):
return(a*a)
def rect(l,b):
return(l*b)
def circle(r):
return(3.14*r*r)
def tri(b,h):
return((b*h)/2)
a=int(input("Enter side: "))
b=int(input("Enter Breadth: "))
l=int(input("Enter length: "))
r=float(input("Enter radius: "))
h=float(input("Enter height: "))
print("area of square = ",square(a))
print("area of rectangle = ",rect(l,b))
print("area of circle = ",circle(r))
print("area of Triangle = ",tri(b,h))

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the program to find the area of different shapes has been executed successfully

using Python.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 5 PRIME NUMBERS LESS THAN 20

Date:

AIM:
To write a python program for executing the prime numbers less than 20.

ALGORITHM:

STEP 1: Start the program

STEP 2: Create a function to check whether a number is prime or not.

STEP 3: Iterate from 2 to 20, and check for prime.

STEP 4: If it is a prime number, print the number.

STEP 5: If the number is not a prime, iterate to the next value

STEP 6: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

print("Prime numbers between 1 and 20 are:")


def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
for i in range(2, 20):
if is_prime(i):
print(i)

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:
Thus the python program for printing the prime numbers less than 20 was executed
successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 6 FACTORIAL USING RECURSION

Date:

AIM:

To write a python program to find the factorial of the given number using recursion
function.
ALGORITHM:

Step 1: Start

Step 2: Read the number num

Step 3: If num<0:

then no value

else:

call Fact(n)

Step 4: Print the factorial

Step 5: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

def fact(n):
if n==1:
return n
else:
return n*fact(n-1)

num=int(input("Enter a number:"))
if num<0:
print("no value")
elif num==0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",fact(num))

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:
Thus the program to find the factorial of the given number using recursion
function has been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 7 ODD NUMBER AND EVEN NUMBER

Date: COUNT CALCULATION

AIM:

To write a python program to count the number of odd and even numbers from array
of N numbers.
ALGORITHM:
Step 1: Start

Step 2: Read the numbers to check if the input number is odd or even

Step 3: If the number is divided by two and gives a remider of zero,then a number is even.

If the remainder is 1,it is an odd number.

Step 4: Print the result

Step 5: Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

import array as arr


numbers=[Link]('i',[1,2,3,4,5,6,7,8,9])
count_odd=0
count_even=0
for i in numbers:
if i%2==0:
count_even+=1
else:
count_odd+=1
print("Number of even numbers:",count_even)
print("Number of odd numbers:",count_odd)

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:
Thus the python program to count the number of odd and even numbers from the
array of numbers has been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 8TE REVERSE A STRING WORD BY WORD

Date:

AIM:

To write a python program to reverse a given string word by word.

ALGORITHM:

STEP 1: Start the program

STEP 2: Split the given string into separate words

STEP 3: Reverse the splitted string list and join using space

STEP 4: Return the joined string

STEP 5: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Read the String value

Declare the String variable

for i in String

str1 = i + str1

Display the reverse


String str1

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

def rev_sentence(sentence):

# first split the string into words


words = [Link](' ')

# then reverse the split string list and join using space
reverse_sentence = ' '.join(reversed(words))

# finally return the joined string


return reverse_sentence

if __name__ == "__main__":
input = 'geeks quiz practice code'
print (rev_sentence(input))

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:
Thus the python program to reverse the string word by word has been executed
successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link].9
E COUNT OCCURENCES OF EACH LIST ELEMENT IN THE TUPLE

Date:
Date:
AIM :

To write a python program to count the occurrences of all items of the list in the tuple

using the given list and tuple.

ALGORITHM:

STEP 1: Start the program

STEP 2: Initialize a tuple tup and a list lst.

STEP 3: Create a list comprehension that loops through each element i in lst and counts the

number of occurrences of i in the tup using the count() method.

STEP 4: Compute the sum of the counts obtained in step 2.

STEP 5: Assign the result of step 3 to the variable count.

STEP 6: Print the value of the count variable.

STEP 7: Stop the program.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Initialize a tuple and a list

Create a list comprehension that


loops through each element

Count the number of


occurences in tup

Compute the sum of


counts in tup and lst

Assign the result of step


3 to the count variable

Print the value of count variable

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

def countOccurrence(tup, lst):


dct = {}
for i in tup:
if not [Link](i):
dct[i] = 0
dct[i] += 1
return sum([Link](i, 0) for i in lst)

tup = ('a', 'a', 'c', 'b', 'd')


lst = ['a', 'b']
print(countOccurrence(tup, lst))

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the python program to count the occurrences of all items of the list in the tuple

using the given list and tuple.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 10TE IINCREASE THE BALANCE IN SAVINGS ACCOUNT THROUGH


RATE OF INTEREST
Date:

AIM:

To write a python program to create a Savings Account and a method to increase the

balance by the appropriate amount of interest using Inheritance.

ALGORITHM:

STEP 1: Start the program

STEP 2: Create a base class called “Bank_Account”.

STEP 3: Create a derived class called “Savings_Account” that inherits the base class.

STEP 4: Add a new method called “Interest rate” in the derived class.

STEP 5: Insert another method in the derived class for increasing the balance by using the

appropriate amount of interest.

STEP 6: Execute the result.

STEP 7: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Read the variables balance,amount

Create a base class

Create a derived class that inherits


the base class

Add a “Interest_rate” method in the


derived class

Insert “add_interest” method in the


derived class

Execute the result

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:
class BankAccount:
def __init__(self,balance=0):
[Link] = balance
def deposit(self,amount):
[Link] += amount
def withdraw(self,amount):
if amount <= [Link]:
[Link] -= amount
else:
print("Insufficient balance")
def get_balance(self):
return [Link]
class SavingsAccount(BankAccount):
def __init__(self,balance=0,interest_rate=0):
super().__init__(balance)
self.interest_rate = interest_rate
def add_interest(self):
interest = [Link] * self.interest_rate
[Link](interest)
def main():
account1 = BankAccount(100) #BankAccount(100)
[Link](50)
[Link](25)
print("BankAccount balance:", account1.get_balance()) #125
account2 = SavingsAccount(200,0.05)
[Link](100)
[Link](50)
account2.add_interest()
print("SavingsAccount balance:",account2.get_balance()) #262.50
if __name__ == "__main__":
main()

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the python program to create a Savings Account and a method to increase the

balance by the appropriate amount of interest using Inheritance has been executed

successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 11TE COPY THE FILE CONTENTS

Date:

AIM:

To write a python program to read a file content and copy only the contents of odd

lines into a new file.

ALGORITHM:

STEP 1: Start the program

STEP 2: Create the two file objects namely f1 for reading and f2 for writing.

STEP 3: Read the contents of f1 and find it as odd or even by using condition.

STEP 4: If it is odd line, then write the content to f2.

STEP 5: Close the file

STEP 6: Open the file f2 in read mode.

STEP 7: Read the content of f2 and display them.

STEP 8: Close both the files

STEP 9: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Create a file object fl for reading

Create a file object f2 for writing

Read the lines from f1

for i in range 0,len(lines in f1)

No
if
i%2 == 0

Yes

Write the content into file f2

pass

Close files f1 and f2

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

f1=open('[Link]','r')
f2=open('[Link]','w')
cont=[Link]()
type(cont)
for i in range(0,len(cont)):
if(i%2==0):
[Link](cont[i])
else:
pass
#close the second file
[Link]()
#open the second file in read mode
f2=open('[Link]','r')
#read the content of the file
cont1=[Link]()
#print the content of the file
print(cont1)
#close all files
[Link]()
[Link]()

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the python program to read a file content and copy lines of odd content into a

new file has been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 12TE TURTLE GRAPHICS WINDOW CREATION

Date:

AIM:

To write a python program to create a turtle graphics window with specific size.

ALGORITHM:

STEP 1: Start the program

STEP 2: Import the turtle module

STEP 3: Create a turtle object.

STEP 4: Define the size of the screen with height and width attribute.

STEP 5: Exit the turtle window

STEP 6: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Import turtle module

Create a window

Set up the dimensions

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

import turtle as t
[Link]('Black')
[Link](3)
[Link](300)
for i in range(350):
[Link](i*4)
[Link](91)
[Link]('aqua')
[Link]()

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the python program to create a turtle graphics window with specific size has

been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 13TE TOWER OF HANOI USING RECURSION

Date:

AIM:

To write a python program for Tower of Hanoi using Recursion.

ALGORITHM:

STEP 1: Start the program

STEP 2: Read the number of disks from the user

STEP 3: Define the recursive function namely Tower of Hanoi

STEP 4: Call the function “Tower of Hanoi” with the number of disks and A,B,C rods.

STEP 5: Check if the number of disks are equal to one,then disk 1 is more from source rod

of the destination rod

STEP 6: Otherwise, call the function recursively until at the disks are arranged in order.

STEP 7: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Define a function

Read number of rings

Define a function

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

If n==1

Print move disc1 from


source to destination

Call the function with n-1


rings,S,I,D

Print move disc n from


source to destination

Call function with n-1 rings,I,D,S

sReturn

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

def TowerOfHanoi(n,from_rod,to_rod,aux_rod):

if n==1:

print("Move disk1 from rod",from_rod,"to rod",to_rod)

return

TowerOfHanoi(n-1,from_rod,aux_rod,to_rod)

print("Move disk",n,"from rod",from_rod,"to rod",to_rod)

TowerOfHanoi(n-1,aux_rod,to_rod,from_rod)

disks=int(input('Enter number of disks: '))

TowerOfHanoi(disks,'A','B','C')

#A,C,B are the name of rods

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT :

Thus the python program for executing the Tower of Hanoi using Recursion has been

successfully executed.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 14TE MENU DRIVEN USING DICTIONARY

Date:

AIM:

To create a menu driven python program with a dictionary for words and their

meanings.

ALGORITHM:

STEP 1: Start the program

STEP 2: Create the dictionary

STEP 3: Import the module json

STEP 4: Import the get_close_matches function from difflib module.

STEP 5: Load the dictionary to the variable data

STEP 6: Define the function translate

STEP 7: Check if the word is in dictionary,return the respective key value

STEP 8: Otherwise, display the word is not in the dictionary

STEP 9: Read the word from the user

STEP 10: Call the translate function and store the result in the output variable and display it

STEP 11: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

import json
from difflib import get_close_matches
#Loading data from json file in python dictionary
data=[Link](open("[Link]"))
def translate(w):
#converts to lower case
w=[Link]()
if w in data:
return data[w]
elif len(get_close_matches(w,[Link]()))>0:
yn=input("Did you mean % s instead? Enter Y if yes,or N if no:") %
get_close_matches(w,[Link]())[0])
yn=[Link]()
if yn=="y":
return data[get_close_matches(w,[Link]())[0]]
elif yn=="n":
return "The word doesn't [Link] double check it."
else:
return "We didn't understand your entry."
else:
return "The word doesn't [Link] double check it."
word=input("Enter word: ")
output=translate(word)
if type(output)==list:
for item in output:
print(item)
else:
print(output)
input('Press ENTER to exit')

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

#[Link]
{"Software":"The program and other operating information used by a
computer","Module":"Collection of source files",
"Exception":"An Exception is an event,which occurs during the execution of a program that
disrupts the normal flow of the program instruction",
"String":"OOPS is a programming paradigm based on the concepts of objects,which can
contain data and code"}

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Read a word

Open the dictionary

Search for close match

No
if found Return not
found

Yes

Return the answer

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the program to create a menu driven using python with a dictionary for words

and their meanings has been executed successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

[Link]: 15TE HANGMAN GAME

Date:

AIM:

To write a python program to implement the hangman game.

ALGORITHM:

STEP 1: Start the program

STEP 2: Import the time module

STEP 3: Read the player name

STEP 4: Assign the secret word to be found

STEP 5: Assign the maximum number of turns

STEP 6: Repeat the step until the turns is less than zero

STEP 7: Initialize the counter variable failed to zero

STEP 8: Loop the statements for each character in the word

STEP 9: Check if char is in guesses, then display

STEP 10: Otherwise, display a dash and increment the counter value

STEP 11: Check if the counter value is equal to zero, then the player wins

STEP 12: Check if the turns value is equal to zero, then the player loses

STEP 13: Stop the program

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

FLOWCHART:

Start

Declare a secret word

Set the number of turns

While
turns > 0

Let the user guess a letter

No
If Decrement the turns
matches

Yes
Yes No
If turns
Print the letter <= 0

No
If word
matches

Yes

Print you won

Stop

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

PROGRAM:

import time
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
[Link](1)
print ("Start guessing...")
[Link](0.5)
word = ("secret")
guesses = ''
turns = 10
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print (char,end=""),
else:
print ("_",end=""),
failed += 1
if failed == 0:
print ("You won")
break
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print ("Wrong")
print ("You have", + turns, 'more guesses')
if turns == 0:
print("You Lose")

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

OUTPUT:

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])


lOMoARcPSD|15090685

RESULT:

Thus the program to implement the Hangman game using Python has been executed

successfully.

Downloaded by Nagalakshmi R (nagalakshmi0273@[Link])

You might also like