@@ -40,8 +40,8 @@ carried out through a CLI._
4040## Building a Simple CLI
4141
4242Before we dive into best practices for Python CLIs, let's build a very simple
43- CLI. Fork this lesson from GitHub and open up ` lib/ grade_reports.py` to follow
44- along.
43+ CLI. Fork and clone this lesson from GitHub and open up ` grade_reports.py `
44+ from the ` lib/ ` directory to follow along.
4545
4646The first thing that we need to do is scaffold our CLI so that it can be run
4747from the command line. To do this, we'll need to create a script. There are two
@@ -58,15 +58,15 @@ if __name__ == '__main__':
5858Remember that the shebang tells the command line that this program should be
5959executed using the Python 3 interpreter. We can technically still run this as a
6060script without it, but that would require us to write
61- ` python lib/ grade_reports.py ` every time we wanted to do so. Since other
61+ ` python grade_reports.py ` every time we wanted to do so. Since other
6262programmers using your CLI might not know about this requirement, you should
63- always include the shebang. Run ` chmod +x lib/grade_reports.py ` to make your
64- script executable.
63+ always include the shebang. In the ` lib/ ` directory, run
64+ ` chmod +x grade_reports.py ` to make your script executable.
6565
6666The ` if __name__ == '__main__' ` block tells the interpreter that this script
67- should only be run if ` lib /grade_reports.py` itself is being called from the
67+ should only be run if ` . /grade_reports.py` itself is being called from the
6868command line. This is important if you want to import any objects from
69- ` lib/ grade_reports.py` into other modules- if you don't include this code block,
69+ ` grade_reports.py ` into other modules- if you don't include this code block,
7070the full script will be run whenever your other module runs the import. That's
7171not likely to be a helpful feature in your CLI.
7272
@@ -78,15 +78,15 @@ produce a grade report a full class of students; we're going to use Python's
7878# !/usr/bin/env python3
7979
8080def create_grade_report (student_grades ):
81- with open (' lib /grade_report.txt' , ' w' ) as gr:
81+ with open (' reports /grade_report.txt' , ' w' ) as gr:
8282 gr.write(student_grades)
8383
8484if __name__ == ' __main__' :
8585 student_grades = input (" Student name, grade: " )
8686 create_grade_report(student_grades)
8787```
8888
89- Run ` lib /grade_reports.py` to execute your script:
89+ Run ` . /grade_reports.py` to execute your script:
9090
9191``` console
9292Student name, grade: Ben, F
@@ -102,7 +102,7 @@ grades line-by-line:
102102# !/usr/bin/env python3
103103
104104def create_grade_report (student_grades ):
105- with open (' lib /grade_report.txt' , ' w' ) as gr:
105+ with open (' ./reports /grade_report.txt' , ' w' ) as gr:
106106 for grade in student_grades:
107107 # add '\n' to write grades on separate lines
108108 gr.write(grade + ' \n ' )
@@ -142,7 +142,7 @@ Student name, grade: Katie, A
142142Student name, grade: # hit enter to complete
143143```
144144
145- Check the ` reports ` directory again- nowyou should see that ` grade_report.txt `
145+ Check the ` reports/ ` directory again- nowyou should see that ` grade_report.txt `
146146has been regenerated and contains grades for all of your students!
147147
148148There are still, of course, many ways to improve this CLI. The instructions
@@ -198,6 +198,9 @@ may find it necessary at times to include `for` loops, `while` loops, and
198198` if/elif/else ` statements in this portion of the CLI, but make sure to separate
199199and organize your code into classes and functions whenever possible.
200200
201+ To make this even clearer, you can separate related functions and classes into
202+ different files and ** import** them into your CLI script.
203+
201204### Validate User Input
202205
203206You may have noticed in our ` grade_reports ` CLI that we did _ not_ validate user
0 commit comments