Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run your first Python program #2

Closed
github-learning-lab bot opened this issue Jan 5, 2021 · 2 comments
Closed

Run your first Python program #2

github-learning-lab bot opened this issue Jan 5, 2021 · 2 comments

Comments

@github-learning-lab
Copy link

Now you're ready to start coding. Let's get familiar with the files in our repo:

  • README.md: a markdown introduction to this project
  • get-quote.py: the file where we'll write our Python code
  • quotes.txt: a text file with a list of quotes

Open up get-quote.py and comment out line 2 by removing the # from the beginning of the line. It will look like this:

  print("Keep it logically awesome.")

The two spaces (or one tab) in front of the line is important. Python uses whitespace to organize code. This print line is part of the main() function. But more on that in the next step. First, let's try running that Python script.

Use the Python 3 command to run the script. From the command line, type one of the following:

  • python get-quote.py
  • python3 get-quote.py

You should see our first quote, the one hard-coded into line 2, printed out in your terminal:
Keep it logically awesome.

Push your changes

You've edited your local code, so you have a more recent version than is stored in this repository. You can check that any time by running: git status

It should show one file modified. Every time we want to send our local changes to GitHub, we need to perform three steps:

  1. Add the file(s) with changes: git add get-quote.py
  2. Commit the changes: git commit -m "Hello World"
  3. Push the changes: git push

Once you've completed these steps, we'll write some more Python.

@github-learning-lab
Copy link
Author

🎉 Good job! You just pushed your first Python code! Now let's try making some changes...

Notice that the first and last lines of the Python file reference main(). That's a Python function and it runs automatically every time you call the script. But only because we told it to.

Try changing the name to something else--almost anything, as long as it doesn't have spaces and isn't a reserved word.

Maybe use a synonym of main, such as primary.

The important thing is to change it in both places, the first line and the last line.

Now run your code again: python get-quote.py or python3 get-quote.py

If you get an error, you might have only changed main in one place, or removed the important () from after the name. You'd also get an error if you changed that __main__ thing (line 10), so leave that one be.

Push your changes

When your Python script is running, you'll see the quote again.

To move on to the next step, push your changes:

  • git add get-quote.py
  • git commit -m "Renamed the primary function"
  • git push

When I see the push come through, I'll share your next steps!

@github-learning-lab
Copy link
Author

Learning the things you can change and the things you can't is a big part of programming.

Next we'll read from our quotes file, taking a step toward building the quote bot.

You can track your progress in this new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants