Page 379 - Computer Science Class 11 Without Functions
P. 379

Program 16

            Write a program that takes a list myList and a value key to be searched in myList as inputs. When the key is
            present in myList, the program prints the index where the search key is found. If the program reaches the end
            of the list without finding the key, it prints an appropriate message indicating absence of the key in the list.

            Ans. '''

                 Objective: To search for a key in a list
                 Input: list and key to be search
                 Output: message indicating the presence or absence of key in list
                 '''


                 myList = eval(input('Enter a list:\n'))
                 print('List:', myList)

                 while True:
                     key = int(input('Enter the number to be searched:'))
                     found = False

                     for index in range(len(myList)):
                         if myList[index] == key:
                             found = True

                             break
                     if found:
                         print(key,' is present in the list at index', index)
                     else:

                         print(key,' is not present in the list')
                      searchAgain = input('Continue another search? say Y/y for yes, N/n for
                      no:')
                     if searchAgain != 'Y' and searchAgain != 'y':

                         break
            Program 17
            Write a program that takes a tuple myTuple and a value key to be searched in myTuple as inputs. When the key
            is present in myTuple, the program prints the index where the search key is found. If the program reaches the
            end of the tuple without finding the key, it prints an appropriate message indicating absence of the key in the tuple.

            Ans. '''
                 Objective: To search for a key in a tuple
                 Input: tuple and key to be search

                 Output: message indicating the presence or absence of key in tuple
                 '''


                 myTuple = eval(input('Enter a tuple:\n'))
                 print('tuple:', myTuple)


                                                                                                      Practical  377
   374   375   376   377   378   379   380   381   382   383   384