IT 116: Introduction to Scripting Class 9 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`
• Create a directory for this exercise
`mkdir  ex9`
• Check that the directory was created
`ls`
• Go to this new directory
`cd ex9`

Practice in interactive mode

• Enter Python interactive mode
`python3`
• Write a `while` loop
```>>> numb = 0
>>> while numb < 10:
...     numb = numb + 1
...
>>> numb```
• Write a `while` loop to add the numbers from 1 to 100
```>>> num = 0
>>> total = 0
>>> while num <= 100:
...     num = num + 1
...     total = total + num
...
>>> total
5151```
• Write a data validation loop
```>>> done = False
>>> while not done:
...     num = int(input('Please enter a number greater than 0: '))
...     if num > 0:
...             done = True
...     else:
...             print(num, 'is not greater than 0')
... ```
• Write an infinite loop
```>>> while True:
...     print("Are we there yet?")```
You many have to hit Control C many times to stop this loop.
• Quit interactive mode

Control D

Practice with scripts

• Create the script ex9.py
`nano  ex9.py`
• Enter the following text into the script file
• Run this script
`python3  ex9.py`
• You should see something like this
```Entering while loop
number: 0
number: 1
number: 2
number: 3
number: 4
number: 5
number: 6
number: 7
number: 8
number: 9
Left while loop
number: 10

The sum of all the numbers from 1 to 100 is 5050

Are we there yet? no
Are we there yet? no
Are we there yet? no
Are we there yet? yes
Yeah!