-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzad21.py
More file actions
39 lines (29 loc) · 919 Bytes
/
zad21.py
File metadata and controls
39 lines (29 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Napisz program, który wczyta pliki utworzone w poprzednim
# zadaniu i obliczy sumę oraz średnią arytmetyczną liczb
# znajdujących się w każdym z nich.
import os
def getNumbers(file):
lines = file.readlines()
for i in range(len(lines)):
try:
lines[i] = int(lines[i].replace("\n", ""))
except(ValueError):
print("Błąd w pliku")
quit()
return lines
num = ""
if(not(os.path.exists(os.getcwd() + "\\numbers"))):
print("Folder 'numbers' nie istnieje w tej lokalizacji.")
evenSum = 0
oddSum = 0
evenNums = []
oddNums = []
with open('numbers/even.txt', 'r') as even, open('numbers/odd.txt', 'r') as odd:
evenNums = getNumbers(even)
oddNums = getNumbers(odd)
for i in evenNums:
evenSum += i
for i in oddNums:
oddSum += i
print(f"Parzyste:\nSuma: {evenSum}, Średnia: {evenSum/len(evenNums)}")
print(f"Nieparzyste:\nSuma: {oddSum}, Średnia: {oddSum/len(oddNums)}")