Page 453 - Computer Science V2.0 Class 11
P. 453

2.  Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is
                      present in the tuple or not.
                      We can accomplish these by:
                      a.  writing a user defined function
                      b.  using the built-in function

                  Ans.  a.  def searchName(nameTuple, name):
                              if name in nameTuple:
                                  return True
                              else:
                                  return False

                          num = int(input("How many students :"))
                          nameTuple = ()
                          for i in range(1, num + 1):
                              name = input("Enter Name : ")
                              nameTuple += (name,)

                          name = input("Enter name to search : ")

                          if searchName(nameTuple, name):
                              print("Found")
                          else:
                              print("Not Found")
                      b.  num = int(input("How many students :"))
                          nameTuple = ()
                          for i in range(1, num + 1):
                              name = input("Enter Name : ")
                              nameTuple += (name,)

                          name = input("Enter name to search : ")
                          if nameTuple.count(name)> 0:
                              print("Found")
                          else:
                              print("Not Found")
                    3.  Write a Python program to find the highest 2 values in a dictionary.
                  Ans.  def findHighestTwoValues(myDict):
                          values = list(myDict.values())
                          values.sort(reverse=True)

                          if len(values) >= 2:
                              highestTwo = values[:2]
                              return highestTwo
                          else:
                              return values

                      def main():
                          studentScores = {
                              'Alice': 85,
                              'Bob': 92,
                              'Charlie': 78,
                              'David': 95,


                                                                                                        Dictionaries  439
   448   449   450   451   452   453   454   455   456   457   458