Python Slip 18

1348d2f1a62b47a0b5679450101d840a

Q1. Write a python program to count the number of characters in a string without using any built-in function.

def findLength(string):
    count = 0
 
    for i in string:
        count += 1
    return count
 
string = input("Enter The String")
print("NUmber of Characters in string are",findLength(string))

OUTPUT

Enter The StringRahul Sonawane
NUmber of Characters in string are 14

Q2. Define a class Person having members – name, address. Create a subclass called ―Employee with member staffid, salary. Create ‘n’ objects of the Employee class and display all the details of highest salaried employee.

class Employee:
    def AcceptEmp(self):
        self.Id=int(input("Enter emp id:"))
        self.Name=input("Enter emp name:")
        self.Dept=input("Enter emp Dept:")
        self.Sal=int(input("Enter emp Salary:"))
    def DisplayEmp(self):
        print("Emp id:",self.Id)
        print("Emp Name:",self.Name)
        print("Emp Dept:",self.Dept)
        print("Emp Salary:",self.Sal)
class Manager(Employee):
    def AcceptMgr(self):
        self.bonus=int(input("Enter Manager Bonus"))
    def DisplayMgr(self):
        print("Manger Bonus is:",self.bonus)
        self.TotalSal=self.Sal+self.bonus
        print("Total Salary: ", self.TotalSal)
n=int(input("Enter How may Managers:"))
s=[]
for i in range(0,n):
    x=input("Enter Object Name:")
    s.append(x)
    print(s)
for j in range(0,n):
    s[j]=Manager()
    s[j].AcceptEmp()
    s[j].AcceptMgr()
for k in range(0,n):   
    print("\nDisplay Details of Manager",k+1)
    s[k].DisplayEmp()
    s[k].DisplayMgr()

maxTotalSal= s[0].TotalSal
maxIndex=0
for j in range(1,n):
    if s[j].TotalSal > maxTotalSal:
        maxTotalSal= s[j].TotalSal
        maxIndex=j
        print("\nMaximum Salary(Salary+Bonus)")
        s[maxIndex].DisplayEmp()
        s[maxIndex].DisplayMgr()

#Check OutPut on Slip 11

Q2. Write a python program to check if a given key already exists in a dictionary. If key exists replace with another key/value pair.

def checkKey(dic, key):
    print("Given Dictionary is:-", dic);
    if key in dic.keys():
        print("Key Present, ", end =" ")
        print("value =", dic[key])
        del(dic[key])
        k=(input("Enter New Key"))
        v=int(input("Enter New Value"))
        dic[k]=v
        print("New Dictionary is:-", dic);
    else:
        print("Key Not present")
         
dic = {'a': 100, 'b':200, 'c':300}
key = 'b'
checkKey(dic, key)

OUTPUT

Given Dictionary is:- {'a': 100, 'b': 200, 'c': 300}
Key Present,  value = 200
Enter New Keyk
Enter New Value101
New Dictionary is:- {'a': 100, 'c': 300, 'k': 101}

0 Comments:

Post a Comment