3.2.5.10 Lab – Writing Scripts Using a Text Editor (Answers)

3.2.5.10 Lab – Writing Scripts Using a Text Editor (Instructor Version)

Topology

3.2.5.10 Lab - Writing Scripts Using a Text Editor (Answers) 3

Objectives

  • Access PL-App from your Netacad.com class.
  • Connect to the Shell on the board using the PL-App Web Shell
  • Create a Program Using Nano

Background / Scenario

Writing scripts using a command line editor is a skill that is often used when managing servers and remote devices. This lab introduces the Nano text editor which is available through the BASH shell.

Required Resources

  • PC with Internet Access
  • Ethernet based connection to the Internet with DHCP and no traffic filtering
  • Raspberry Pi that is configured and imaged for PL-App access

Part 1: Accessing the Shell Interface using PL-App

Step 1: Connect to the Linux Shell using the PL-App Web Shell.

a. Click the New menu in PL-App from the menu and select Terminal:

3.2.5.10 Lab - Writing Scripts Using a Text Editor (Answers) 4

b. A new window will open with the Linux shell running on the Raspberry Pi board.

Step 2: Create a Program Using Nano

1. Open the text editor Nano by typing nano GuessAgain.py in the command line. If the GusessAgain.py file already exists in the current directory, nano will open the file for editing. If the file does not exist, it will create it. Note: Filenames are case sensitive in Linux, so make sure you type in the same case each time.

2. To ensure that the script will be executed by the Python (version 3) interpreter, the very first line of the script needs to start with the shebang #! characters followed by the path/location of the interpreter (the env util selects the Python executable that is active in the current $PATH):

#!/usr/bin/env python3

3. Enter the remaining code into the GuessAgain.py file. Once complete, save the file and exit the editor by pressing CTRL+x and selecting Y, for yes, to save the file then Enter to accept the current filename.

#!/usr/bin/env python3
C="X"           #C represents Correct
G=1             #G represents Guesses
L=0             #L represents Low
H=1000          #H represents High
print ("Think of a number between 1 and 1000 and I will guess it.")
while C != "C":
    print ("Guess Number ",G)
    print ("Is your number ",int((L+H)/2),"? (C)orrect, (L)ower, (H)igher") #make sure it is integer
    C = input(">")
    if C == "L":
            H = (L+H)/2
            G = G + 1
    else:
            if C == "H":
                    L = (L+H)/2
                    G = G + 1
            else:
                    print ("Please enter C,L, or H")
print ("I guessed it in ",G,"tries!")

4. Before you can directly execute the Python code from the shell, you must turn the file into an executable file using the chmod +x command.

Execute the program by typing ./GuessAgain.py.

[email protected]:/home/pi/notebooks# ls -l ./GuessAgain.py
-rw-r--r-- 1 root root 1689 Dec 5 02:24 GuessAgain.py
[email protected]:/home/pi/notebooks# ./GuessAgain.py
bash: ./GuessAgain.py: Permission denied
[email protected]:/home/pi/notebooks# chmod +x GuessAgain.py
[email protected]:/home/pi/notebooks# ls -l ./GuessAgain.py
-rwxr-xr-x 1 root root 1689 Dec 5 02:24 GuessAgain.py
[email protected]:/home/pi/notebooks# ./GuessAgain.py

5. Examine the code from the command line using the cat, more, and less commands.

[email protected]:/home/pi/notebooks# cat GuessAgain.py
[email protected]:/home/pi/notebooks# more GuessAgain.py
[email protected]:/home/pi/notebooks# less GuessAgain.py

Note: Press Q to quit the less command.

Reflection

In the GuessAgain.py program, when C is pressed for correct, the program asks the user to Please enter C,L, or H before quitting. Can you fix this problem?

 


guest
0 Comments
Inline Feedbacks
View all comments