wignition / python-random-quote

A file-based quote bot written in Python
https://lab.github.com/everydeveloper/introduction-to-python
0 stars 0 forks source link

Run your first Python program #2

Closed github-learning-lab[bot] closed 3 years ago

github-learning-lab[bot] commented 3 years ago

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

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:

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[bot] commented 3 years ago

🎉 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:

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

github-learning-lab[bot] commented 3 years ago

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.

wignition commented 3 years ago

MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" Password: ==> This script will install: /usr/local/bin/brew /usr/local/share/doc/homebrew /usr/local/share/man/man1/brew.1 /usr/local/share/zsh/site-functions/_brew /usr/local/etc/bash_completion.d/brew /usr/local/Homebrew ==> The following existing directories will be made group writable: /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> The following existing directories will have their owner set to MurcadomTechnology: /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> The following existing directories will have their group set to admin: /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> The following new directories will be created: /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks ==> The Xcode Command Line Tools will be installed.

Press RETURN to continue or any other key to abort ==> /usr/bin/sudo /bin/chmod u+rwx /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> /usr/bin/sudo /usr/sbin/chown MurcadomTechnology /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/bin /usr/local/share /usr/local/share/man /usr/local/share/man/man1 ==> /usr/bin/sudo /bin/mkdir -p /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks ==> /usr/bin/sudo /usr/sbin/chown MurcadomTechnology /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks ==> /usr/bin/sudo /bin/mkdir -p /Users/MurcadomTechnology/Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Users/MurcadomTechnology/Library/Caches/Homebrew ==> /usr/bin/sudo /usr/sbin/chown MurcadomTechnology /Users/MurcadomTechnology/Library/Caches/Homebrew ==> Searching online for the Command Line Tools ==> /usr/bin/sudo /usr/bin/touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress ==> Installing Command Line Tools for Xcode-12.1 ==> /usr/bin/sudo /usr/sbin/softwareupdate -i Command\ Line\ Tools\ for\ Xcode-12.1 Software Update Tool

Downloading Command Line Tools for Xcode
Downloaded Command Line Tools for Xcode Installing Command Line Tools for Xcode Done with Command Line Tools for Xcode Done. ==> /usr/bin/sudo /bin/rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress Password: ==> /usr/bin/sudo /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools ==> Downloading and installing Homebrew... remote: Enumerating objects: 52, done. remote: Counting objects: 100% (52/52), done. remote: Compressing objects: 100% (52/52), done. remote: Total 157518 (delta 3), reused 46 (delta 0), pack-reused 157466 Receiving objects: 100% (157518/157518), 40.14 MiB | 924.00 KiB/s, done. Resolving deltas: 100% (116516/116516), done. From https://github.com/Homebrew/brew

==> Homebrew has enabled anonymous aggregate formulae and cask analytics. Read the analytics documentation (and how to opt-out) here: https://docs.brew.sh/Analytics No analytics data has been sent yet (or will be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating: https://github.com/Homebrew/brew#donations

==> Next steps:

Troubleshooting: brew config brew doctor brew install --verbose --debug FORMULA

Contributing: brew create [URL [--no-fetch]] brew edit [FORMULA...]

Further help: brew commands brew help [COMMAND] man brew https://docs.brew.sh MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % brew install git Updating Homebrew... ==> Downloading https://homebrew.bintray.com/bottles/gettext-0.21.catalina.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/cdea54f52b7c36ebcb5fe26a1cf736d7cd6fd5f2 ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/pcre2-10.35.catalina.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/6a1e59a5db23d684f92d2bf695601d1b466f3e9d ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/git-2.29.2.catalina.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/47cffc0d84e2a273cc6088802633e1d4d1fc8afe ######################################################################## 100.0% ==> Installing dependencies for git: gettext and pcre2 ==> Installing git dependency: gettext ==> Pouring gettext-0.21.catalina.bottle.tar.gz 🍺 /usr/local/Cellar/gettext/0.21: 1,953 files, 19.0MB ==> Installing git dependency: pcre2 ==> Pouring pcre2-10.35.catalina.bottle.tar.gz 🍺 /usr/local/Cellar/pcre2/10.35: 230 files, 6.0MB ==> Installing git ==> Pouring git-2.29.2.catalina.bottle.tar.gz ==> Caveats The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the git-gui formula.

Bash completion has been installed to: /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to: /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/git ==> Summary 🍺 /usr/local/Cellar/git/2.29.2: 1,480 files, 39.7MB ==> Caveats ==> git The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the git-gui formula.

Bash completion has been installed to: /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to: /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/git MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % git --version git version 2.24.3 (Apple Git-128) MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % git clone https://github.com/wignition/python-random-quote.git Cloning into 'python-random-quote'... remote: Enumerating objects: 24, done. remote: Counting objects: 100% (24/24), done. remote: Compressing objects: 100% (18/18), done. remote: Total 24 (delta 4), reused 24 (delta 4), pack-reused 0 Unpacking objects: 100% (24/24), done. MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % open get-quote.py The file /Users/MurcadomTechnology/get-quote.py does not exist. MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % open /python-random-quote/get-quote.py The file /python-random-quote/get-quote.py does not exist. MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % open ~/python-random-quote/get-quote.py MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % python3 Python 3.9.0 (v3.9.0:9cf6752276, Oct 5 2020, 11:29:23) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information.

python3 get-quote.py File "", line 1 python3 get-quote.py ^ SyntaxError: invalid syntax exit() MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % python3 get-quote.py /Library/Frameworks/Python.framework/Versions/3.9/bin/python3: can't open file '/Users/MurcadomTechnology/get-quote.py': [Errno 2] No such file or directory MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 ~ % cd ~/python-random-quote MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % python3 get-quote.py File "/Users/MurcadomTechnology/python-random-quote/get-quote.py", line 10 if name== "main": ^ IndentationError: expected an indented block MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % python3 get-quote.py File "/Users/MurcadomTechnology/python-random-quote/get-quote.py", line 10 if name== "main": ^ IndentationError: expected an indented block MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % python3 get-quote.py Keep it logically awesome. ['Responsive is better than fast\n', 'It’s not fully shipped until it’s fast\n', 'Anything added dilutes everything else\n', 'Practicality beats purity\n', 'Approachable is better than simple\n', 'Mind your words, they are important\n', 'Speak like a human\n', 'Half measures are as bad as nothing at all\n', 'Encourage flow\n', 'Non-blocking is better than blocking\n', 'Favor focus over features\n', 'Avoid administrative distraction\n', 'Design for failure\n', 'Keep it logically awesome\n'] MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % clear

MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % python3 get-quote.py Keep it logically awesome. ['Responsive is better than fast\n', 'It’s not fully shipped until it’s fast\n', 'Anything added dilutes everything else\n', 'Practicality beats purity\n', 'Approachable is better than simple\n', 'Mind your words, they are important\n', 'Speak like a human\n', 'Half measures are as bad as nothing at all\n', 'Encourage flow\n', 'Non-blocking is better than blocking\n', 'Favor focus over features\n', 'Avoid administrative distraction\n', 'Design for failure\n', 'Keep it logically awesome\n'] MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git status On branch master Your branch is up to date with 'origin/master'.

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: get-quote.py

no changes added to commit (use "git add" and/or "git commit -a") MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git add get-quote.py MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git commit -m "Hello World" [master 658d024] Hello World 1 file changed, 5 insertions(+), 5 deletions(-) MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git push Username for 'https://github.com': wignition Password for 'https://wignition@github.com': Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 430 bytes | 430.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/wignition/python-random-quote.git b180cd1..658d024 master -> master MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % python3 get-quote.py Keep it logically awesome. ['Responsive is better than fast\n', 'It’s not fully shipped until it’s fast\n', 'Anything added dilutes everything else\n', 'Practicality beats purity\n', 'Approachable is better than simple\n', 'Mind your words, they are important\n', 'Speak like a human\n', 'Half measures are as bad as nothing at all\n', 'Encourage flow\n', 'Non-blocking is better than blocking\n', 'Favor focus over features\n', 'Avoid administrative distraction\n', 'Design for failure\n', 'Keep it logically awesome\n'] MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git add get-quote.py MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % get commet -m "Renamed the primary function" zsh: command not found: get MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git commit -m "Renamed the primary function" [master a496d63] Renamed the primary function 1 file changed, 2 insertions(+), 2 deletions(-) MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote % git push Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 365 bytes | 365.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/wignition/python-random-quote.git 658d024..a496d63 master -> master MurcadomTechnology@TIM-AIR9-1306-17ghzi7-8gb120gb-3 python-random-quote %