Skip to main content

Unit 7 · Borrowing Code · Lesson 56 of 57

Back to the Terminal

Back to where it all started

Remember the very first thing you did in this course? You ran python3 helloworld.py in a Linux terminal, without having any idea what was inside it.

Now you're going to write your own.

The final project

Write a password generator, on a real Linux machine, from scratch:

  1. Make a file called passgen.py with nano
  2. Use secrets and string to make a 16 character password of random letters and digits
  3. Print it (just the password, nothing else on that line)
  4. Run it with python3

You already wrote most of this in Secrets Are Safer, so you've got this. It's the same Python, it's just in a file now instead of the editor.

Using nano

In the terminal below, type:

nano passgen.py

That opens a text editor right inside the terminal. Type (or paste) your code in, then:

  • Ctrl + O, then Enter to save (O for "output")
  • Ctrl + X to exit

Then run it:

python3 passgen.py

Indentation still matters in nano! Use 4 spaces to indent. If you get an IndentationError, open the file again with nano passgen.py and fix it.

Run it a few times. You should get a different password every time!

Live Linux terminal

This lesson comes with a real Ubuntu machine that runs in your browser, so you type every command yourself. Create a free account to do this part.

Tasks to do

Do each one in the terminal or on your practice VM, and it checks itself off. Create a free account to do this part.

Do this lesson for real

With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.