Skip to content

Instantly share code, notes, and snippets.

@x1001000
Created May 22, 2021 21:45
Show Gist options
  • Save x1001000/2403c1a96932e611821746c95f99d51e to your computer and use it in GitHub Desktop.
Save x1001000/2403c1a96932e611821746c95f99d51e to your computer and use it in GitHub Desktop.
# 男生人數和女生人數初始值皆設為0
N_males = N_females = 0
with open('read.dat') as f:
for line in f.readlines():
print(line)
N = line.split()[-2]
# 排除檔案第一行切出來的N為'性別'的情況
if N == '0':
N_females += 1
if N == '1':
N_males += 1
print(f'Number of males: {N_males}')
print(f'Number of females: {N_females}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment