Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Activity1.py
- ----------------------------------------------------
- A program that would accept eight(8) positive integers
- not greater than 10,place the valid inputted items to the list,
- count and display the number of odd values and even values of the
- list,display the smallest and the largest of the list,
- display the sum and average of the list, display the list content
- example:
- input eight(8) value (1..10):
- 10
- 4
- 5
- 1
- 6
- 7
- 3
- 8
- odd: 4
- even: 4
- smallest: 1
- largest: 10
- sum: 44
- average: 5.5
- [10,4,5,1,6,7,3,8]
- """
- from os import system
- listVal = [] #initialize the list
- def main()->None:
- system("cls")
- count_input = 0 #create a counter variable for the count of input
- count_Even = 0
- count_Odd = 0
- print("input eight(8) value (1..10):")
- """
- so while loop ang gigamit kay para if dili true and condition continue ra sya sa input
- ex:
- if ang value sa count_input kay < 8 so continue pa sya bisan pag ma wrong ang input
- basta wala pa ni equal sa 8 ang count_input
- """
- while count_input != 8:
- inputNum = int(input())
- # mao ni mo validate if inputNum is greater than or less than 1
- #So is 11 or 0 ang input mao ng mo print ang error
- if inputNum >10 or inputNum <1:
- print("Error")
- # So diri sa else kay if and inputNum is naa sa 1-10
- #diri sya sud
- else:
- #listVal.append(inputNum) i append niya ang input value sa list
- listVal.append(inputNum)
- # then mag increment ug 1 sa count
- count_input += 1
- # mao ni mo check sa even
- if inputNum % 2 == 0:
- count_Even += 1
- # then mag increment ug 1 sa count_Even
- # kani sa odd
- elif inputNum % 2 != 0:
- count_Odd += 1
- #make sure na ang print nimo naka gawas sa loop ug naka indent tarung
- print(f"count of Odd: {count_Odd}")
- print(f"count of Even: {count_Even}")
- print(f"Snallest: {small(listVal)}")
- print(f"Largest: {large(listVal)}")
- print(f"Sum: {sum(listVal)}")#sa pag sum gamit lang us built-in function na sum
- print(f"Average: {(sum(listVal)/len(listVal))}")
- print(f"Values: {listVal}")
- def small(listVal):
- #so diri assume ta nga ang smallest kay ang index 0 or listVal[0]
- small = listVal[0]
- #then e iterate ang list
- # Teaverse list from second and compare every element
- #range(0, len(listVal)) == range(start, end)
- #ang len(listVal) mao ni size sa list
- for i in range(0, len(listVal)):
- if listVal[i] < small:
- small = listVal[i]
- return small
- def large(listVal):
- large = listVal[0]
- for i in range(0, len(listVal)):
- if listVal[i] > large:
- large = listVal[i]
- return large
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement