Advertisement
Kulas_Code20

Sample

Sep 8th, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | Source Code | 0 0
  1. """
  2.    Activity1.py
  3.    ----------------------------------------------------
  4.    A program that would accept eight(8) positive integers
  5.    not greater than 10,place the valid inputted items to the list,
  6.    count and display the number of odd values and even values of the
  7.    list,display the smallest and the largest of the list,
  8.    display the sum and average of the list, display the list content
  9.    example:
  10.    input eight(8) value (1..10):
  11.        10
  12.        4
  13.        5
  14.        1
  15.        6
  16.        7
  17.        3
  18.        8
  19.        odd: 4
  20.        even: 4
  21.        smallest: 1
  22.        largest: 10
  23.        sum: 44
  24.        average: 5.5
  25.        [10,4,5,1,6,7,3,8]
  26. """
  27.  
  28. from os import system
  29. listVal = [] #initialize the list
  30. def main()->None:
  31.      system("cls")
  32.      count_input = 0 #create a counter variable for the count of input
  33.      count_Even = 0
  34.      count_Odd = 0
  35.      print("input eight(8) value (1..10):")
  36.      """
  37.        so while loop ang gigamit kay para if dili true and condition continue ra sya sa input
  38.        ex:
  39.          if ang value sa count_input kay < 8 so continue pa sya bisan pag ma wrong ang input
  40.          basta wala pa ni equal sa 8 ang count_input
  41.     """
  42.      while count_input != 8:
  43.          inputNum = int(input())
  44.          # mao ni mo validate if inputNum is greater than or less than 1
  45.          #So is 11 or 0 ang input mao ng mo print ang error
  46.          if inputNum >10 or inputNum <1:
  47.              print("Error")
  48.          # So diri sa else kay if and inputNum is naa sa 1-10
  49.          #diri sya sud
  50.          else:
  51.              #listVal.append(inputNum) i append niya ang input value sa list
  52.              listVal.append(inputNum)
  53.              # then mag increment ug 1 sa count
  54.              count_input += 1
  55.              # mao ni mo check sa even
  56.              if inputNum % 2 == 0:
  57.                  count_Even += 1
  58.              # then mag increment ug 1 sa count_Even
  59.              # kani sa odd
  60.              elif inputNum % 2 != 0:
  61.                  count_Odd += 1
  62.  
  63.     #make sure na ang print nimo naka gawas sa loop ug naka indent tarung
  64.      print(f"count of Odd: {count_Odd}")
  65.      print(f"count of Even: {count_Even}")
  66.      print(f"Snallest: {small(listVal)}")
  67.      print(f"Largest: {large(listVal)}")
  68.      print(f"Sum: {sum(listVal)}")#sa pag sum gamit lang us built-in function na sum
  69.      print(f"Average: {(sum(listVal)/len(listVal))}")
  70.      print(f"Values: {listVal}")
  71.  
  72. def small(listVal):
  73.     #so diri assume ta nga ang smallest kay ang index 0 or listVal[0]
  74.     small = listVal[0]
  75.     #then e iterate ang list
  76.     # Teaverse list from second and compare every element
  77.     #range(0, len(listVal)) == range(start, end)
  78.     #ang len(listVal) mao ni size sa list
  79.     for i in range(0, len(listVal)):
  80.         if listVal[i] < small:
  81.             small = listVal[i]
  82.     return small
  83.  
  84. def large(listVal):
  85.     large = listVal[0]
  86.     for i in range(0, len(listVal)):
  87.         if listVal[i] > large:
  88.             large = listVal[i]
  89.     return large
  90.  
  91. if __name__ == "__main__":
  92.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement