## IT 116: Introduction to Scripting Class 12 Exercise

• Run an SSH client
Run putty.exe if you have a PC or Terminal if you have a Mac.

### Create a directory for this exercise

• Go to your home directory
cd
• Go to your it116 directory
cd  it116
• Go to your ex directory
cd ex
• Make sure you are in your ex directory
pwd
If you are not in /home/UNIX_ID/it116/ex, see me.
• Create a directory for this exercise
mkdir  ex12
• Check that the directory was created
ls
• Go to this new directory
cd ex12

### Practice in interactive mode

• Enter Python interactive mode
python3
• Create the function cheer_1
>>> def cheer_1(team):
...    print("Go " + team + "!")
...
• Run this function
cheer_1("Red Sox")
• Create a function that uses a global variable
def cheer_2():
...    print("Go " + team + "!")
...
• Run this function
>>> team = "Patriots"
>>> cheer_2()
• Create a function with a default value
def cheer_3(team, times=3):
...    for i in range(times):
...       print("Go " + team + "!")
...
• Run this function with different function calls
>>> cheer_3("Red Sox", 5)
>>> cheer_3("Red Sox", 1)
cheer_3("Red Sox")
• Quit interactive mode

Control D

### Practice with scripts

• Create the script ex12.py
nano  ex12.py
• Enter the following text into the script file
• Run this script
python3  ex12.py
• You should see
Go Red Sox!
Go Patriots!
Go Bruins!
Go Celtics!

Go Patriots!
Go Patriots!

Go Red Sox!
Go Red Sox!
Go Red Sox!

Go Bruins!

1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

1 cubed is 1
2 cubed is 8
3 cubed is 27
4 cubed is 64
5 cubed is 125

1 multiplied by itself 2 times is 1
2 multiplied by itself 2 times is 4
3 multiplied by itself 2 times is 9
4 multiplied by itself 2 times is 16
5 multiplied by itself 2 times is 25