0% found this document useful (0 votes)
5 views8 pages

Python Menu Driven Programs Guide

The document contains multiple Python programs demonstrating various functionalities, including menu-driven programs for list and dictionary management, a function to find prime numbers, interest calculation, finding the largest element in a list, counting vowels in a string, and handling exceptions during addition and square root calculations. Each program is designed to perform specific tasks and includes user input for interaction. The code snippets illustrate basic programming concepts and error handling in Python.

Uploaded by

abhayranjana767
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)
5 views8 pages

Python Menu Driven Programs Guide

The document contains multiple Python programs demonstrating various functionalities, including menu-driven programs for list and dictionary management, a function to find prime numbers, interest calculation, finding the largest element in a list, counting vowels in a string, and handling exceptions during addition and square root calculations. Each program is designed to perform specific tasks and includes user input for interaction. The code snippets illustrate basic programming concepts and error handling in Python.

Uploaded by

abhayranjana767
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

1.

List Menu Driven Program


lst = []
while True:
print("[Link] [Link] [Link] [Link]")
ch = int(input("Enter choice: "))
if ch == 1:
[Link](int(input("Enter element: ")))
elif ch == 2:
x = int(input("Enter element to remove: "))
if x in lst:
[Link](x)
elif ch == 3:
print(lst)
elif ch == 4:
break
2. Dictionary Menu Driven Program
d = {}
while True:
print("[Link] [Link] [Link] [Link]")
ch = int(input("Choice: "))
if ch == 1:
d[input("Key: ")] = input("Value: ")
elif ch == 2:
[Link](input("Key: "), None)
elif ch == 3:
print(d)
elif ch == 4:
break
3. Prime Numbers between m and n
def primes(m, n):
for num in range(m, n+1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)

primes(10, 50)
4. Calculate Interest
def interest(p, r, t=1):
return (p*r*t)/100

print(interest(1000, 5, 2))
5. Largest Element in List
def largest(lst):
return max(lst)

print(largest([3,5,1,9]))
6. Count Vowels in String
def count_vowels(s):
v = "aeiouAEIOU"
return sum(1 for i in s if i in v)

print(count_vowels("Python"))
7. Addition with Exception Handling
try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a+b)
except ValueError:
print("Invalid input")
8. Square Root with Custom Exception
import math
class NegativeError(Exception):
pass

try:
n = int(input("Enter number: "))
if n < 0:
raise NegativeError
print([Link](n))
except NegativeError:
print("Negative number not allowed")

You might also like