Python Slip 7

7122cdb966a446ff8ba6f02ff93465ac

Q1. Write a python program to show use of multiple exception handing.

try:
    # defining variables
    a = 10
    b= 0
    c = "abc"
    d =a/b
# Zerodivision error
except ZeroDivisionError:
    print("Zero Division Error occurs")
# index error
except IndexError:
    print("Index error occurs")
# type error
except TypeError:
    print("Type error occurs")
Zero Division Error occurs

Q2. Write a python program to display tables from m to n.

Example Input: m=3, n=7 Output: 31=3 41=4 …… 71=7 32=6 42=8 …… 72=14 . . . 310=30 410=40 …… 7*10=70

print("Enter the number of which the user wants to print the multiplication table: ")
m = int(input ("Enter the value of M "))
n = int(input ("Enter the value of n "))      
print ("The Multiplication Table is: ")    
for i in range(1, 11):
    for j in range(m,n+1):
        print (j, '*', i, '=', j * i,end="\t")
    print()
Enter the number of which the user wants to print the multiplication table: 
Enter the value of M 3
Enter the value of n 7
The Multiplication Table is: 
3 * 1 = 3	4 * 1 = 4	5 * 1 = 5	6 * 1 = 6	7 * 1 = 7	
3 * 2 = 6	4 * 2 = 8	5 * 2 = 10	6 * 2 = 12	7 * 2 = 14	
3 * 3 = 9	4 * 3 = 12	5 * 3 = 15	6 * 3 = 18	7 * 3 = 21	
3 * 4 = 12	4 * 4 = 16	5 * 4 = 20	6 * 4 = 24	7 * 4 = 28	
3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	6 * 5 = 30	7 * 5 = 35	
3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	7 * 6 = 42	
3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	
3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	
3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	
3 * 10 = 30	4 * 10 = 40	5 * 10 = 50	6 * 10 = 60	7 * 10 = 70	

Q2. Write a python program to accept directory name and print names of all files whose extension is ‘.txt’ in the given directory.

import glob

print("Enter the Extension (eg, .txt, .html, .css etc): ", end="")
e = input()

fileslist = []
for file in glob.glob("*"+e):
    fileslist.append(file)

if len(fileslist)>0:
    print("\nList of All Files with \"" +e+ "\" Extension:")
    for f in fileslist:
        print(f)
else:
    print("\nNot found with \"" +e+ "\" extension!")
Enter the Extension (eg, .txt, .html, .css etc): .txt

List of All Files with ".txt" Extension:
mysenti.txt
output.txt
samplefile.txt
test.txt
test1.txt

0 Comments:

Post a Comment