Commit 75bcf6c8 authored by Stephen Pearce's avatar Stephen Pearce Committed by Maxim Belkin

10-cmdline.md: Update loop variable names (#743)

parent b87cbabc
......@@ -163,12 +163,13 @@ $ cat ../code/readings_01.py
import sys
import numpy
def main():
script = sys.argv[0]
filename = sys.argv[1]
data = numpy.loadtxt(filename, delimiter=',')
for m in numpy.mean(data, axis=1):
print(m)
for row_mean in numpy.mean(data, axis=1):
print(row_mean)
~~~
{: .language-python}
......@@ -199,8 +200,8 @@ def main():
script = sys.argv[0]
filename = sys.argv[1]
data = numpy.loadtxt(filename, delimiter=',')
for m in numpy.mean(data, axis=1):
print(m)
for row_mean in numpy.mean(data, axis=1):
print(row_mean)
if __name__ == '__main__':
main()
......@@ -399,8 +400,8 @@ def main():
script = sys.argv[0]
for filename in sys.argv[1:]:
data = numpy.loadtxt(filename, delimiter=',')
for m in numpy.mean(data, axis=1):
print(m)
for row_mean in numpy.mean(data, axis=1):
print(row_mean)
if __name__ == '__main__':
main()
......@@ -456,8 +457,8 @@ def main():
action = sys.argv[1]
filenames = sys.argv[2:]
for f in filenames:
data = numpy.loadtxt(f, delimiter=',')
for filename in filenames:
data = numpy.loadtxt(filename, delimiter=',')
if action == '--min':
values = numpy.min(data, axis=1)
......@@ -466,8 +467,8 @@ def main():
elif action == '--max':
values = numpy.max(data, axis=1)
for m in values:
print(m)
for val in values:
print(val)
if __name__ == '__main__':
main()
......@@ -520,8 +521,8 @@ def main():
filenames = sys.argv[2:]
assert action in ['--min', '--mean', '--max'], \
'Action is not one of --min, --mean, or --max: ' + action
for f in filenames:
process(f, action)
for filename in filenames:
process(filename, action)
def process(filename, action):
data = numpy.loadtxt(filename, delimiter=',')
......@@ -533,8 +534,8 @@ def process(filename, action):
elif action == '--max':
values = numpy.max(data, axis=1)
for m in values:
print(m)
for val in values:
print(val)
if __name__ == '__main__':
main()
......@@ -625,8 +626,8 @@ def main():
if len(filenames) == 0:
process(sys.stdin, action)
else:
for f in filenames:
process(f, action)
for filename in filenames:
process(filename, action)
def process(filename, action):
data = numpy.loadtxt(filename, delimiter=',')
......@@ -638,8 +639,8 @@ def process(filename, action):
elif action == '--max':
values = numpy.max(data, axis=1)
for m in values:
print(m)
for val in values:
print(val)
if __name__ == '__main__':
main()
......@@ -773,6 +774,7 @@ the program now does everything we set out to do.
>
> > ## Solution
> > ~~~
> > # this is code/readings_07.py
> > import sys
> > import numpy
> >
......@@ -785,8 +787,8 @@ the program now does everything we set out to do.
> > if len(filenames) == 0:
> > process(sys.stdin, action)
> > else:
> > for f in filenames:
> > process(f, action)
> > for filename in filenames:
> > process(filename, action)
> >
> > def process(filename, action):
> > data = numpy.loadtxt(filename, delimiter=',')
......@@ -798,8 +800,8 @@ the program now does everything we set out to do.
> > elif action == '-x':
> > values = numpy.max(data, axis=1)
> >
> > for m in values:
> > print(m)
> > for val in values:
> > print(val)
> >
> > main()
> > ~~~
......@@ -836,8 +838,8 @@ the program now does everything we set out to do.
> > if len(filenames) == 0:
> > process(sys.stdin, action)
> > else:
> > for f in filenames:
> > process(f, action)
> > for filename in filenames:
> > process(filename, action)
> >
> > def process(filename, action):
> > data = numpy.loadtxt(filename, delimiter=',')
......@@ -849,8 +851,8 @@ the program now does everything we set out to do.
> > elif action == '--max':
> > values = numpy.max(data, axis=1)
> >
> > for m in values:
> > print(m)
> > for val in values:
> > print(val)
> >
> > main()
> > ~~~
......@@ -866,6 +868,7 @@ the program now does everything we set out to do.
>
> > ## Solution
> > ~~~
> > # this is code/readings_09.py
> > import sys
> > import numpy
> >
......@@ -881,8 +884,8 @@ the program now does everything we set out to do.
> > if len(filenames) == 0:
> > process(sys.stdin, action)
> > else:
> > for f in filenames:
> > process(f, action)
> > for filename in filenames:
> > process(filename, action)
> >
> > def process(filename, action):
> > data = numpy.loadtxt(filename, delimiter=',')
......@@ -894,8 +897,8 @@ the program now does everything we set out to do.
> > elif action == '--max':
> > values = numpy.max(data, axis=1)
> >
> > for m in values:
> > print(m)
> > for val in values:
> > print(val)
> >
> > main()
> > ~~~
......@@ -923,12 +926,12 @@ the program now does everything we set out to do.
> > else:
> > nrow0, ncol0 = row_col_count(filenames[0])
> > print('First file %s: %d rows and %d columns' % (filenames[0], nrow0, ncol0))
> > for f in filenames[1:]:
> > nrow, ncol = row_col_count(f)
> > for filename in filenames[1:]:
> > nrow, ncol = row_col_count(filename)
> > if nrow != nrow0 or ncol != ncol0:
> > print('File %s does not check: %d rows and %d columns' % (f, nrow, ncol))
> > print('File %s does not check: %d rows and %d columns' % (filename, nrow, ncol))
> > else:
> > print('File %s checks' % f)
> > print('File %s checks' % filename)
> > return
> >
> > def row_col_count(filename):
......@@ -969,10 +972,10 @@ the program now does everything we set out to do.
> > sum_nlines = count_file_like(sys.stdin)
> > print('stdin: %d' % sum_nlines)
> > else:
> > for f in filenames:
> > n = count_file(f)
> > print('%s %d' % (f, n))
> > sum_nlines += n
> > for filename in filenames:
> > nlines = count_file(filename)
> > print('%s %d' % (filename, nlines))
> > sum_nlines += nlines
> > print('total: %d' % sum_nlines)
> >
> > def count_file(filename):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment