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

'''
                  Objective: To read numbers representing ages and mean, count, and  mode of all ages.
                  Input: a list of age
                  Output: count, mean, and the mode of list of age
                  '''
                  ageList = []
                  #Accept data from the user
                  more = 'y'
                  while more == 'y' or more == 'Y':
                      age = int(input("Enter the age : "))
                      ageList.append(age)
                      more = input("Do you wish to enter more (y/n): ")
                  #Process data entered by the user
                  print("Count of friends: ", len(ageList))
                  #round age to one place after decimal
                  print("Average age: ", round(statistics.mean(ageList), 1))
                  print("Most frequently occuring age: ", statistics.mode(ageList))
               3.  While Shamitabh's teacher appreciates him for being able to exploit the capabilities of Python, she wants him to develop
                  his own program to carry out the desired operations. Help Shamitabh one more time.
             Ans.  Approach:
                  a.  Prompt the user to enter age one by one and append to an initially empty list.
                  b.  If the user reponds by y or Y, continue, else stop and process data.
                  c.  Use meanList, countElements and modeList to find mean, count, and the mode of the elements of list.
                  #Objective: To read numbers representing ages and mean, count, and mode of all ages.
                  more = 'y'
                  ageList = []
                  while more == 'y' or more == 'Y':
                      age = int(input("Enter the age : "))
                      ageList.append(age)
                      more = input("Do you wish to enter more (y/n): ")



                  '''
                  Objective: To compute the mean of the elements of a list
                  Input: a list of numbers
                  Output: mean of list
                  '''
                  total = 0
                  count = 0
                  for element in ageList:
                      total += element
                      count += 1
                  meanLst = total / count
                  print("Average age is: ", meanLst)



                                                                                                Lists and Tuples  313
   310   311   312   313   314   315   316   317   318   319   320