python3
while
loop>>> numb = 0 >>> while numb < 10: ... numb = numb + 1 ... >>> numb
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
>>> 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') ...
>>> while True: ... print("Are we there yet?")You many have to hit Control C many times to stop this loop.
Entering while loop number: 0 number: 1 number: 2 number: 3 Left while loop number: 4 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! Please enter an integer greater than 0: -1 -1 is not greater than 0 Please enter an integer greater than 0: 0 0 is not greater than 0 Please enter an integer greater than 0: 5 5
cd it116
cd ex
mkdir ex9
ls
cd it116/ex/ex9
python3 ex9.py
Entering while loop number: 0 number: 1 number: 2 number: 3 Left while loop number: 4 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! Please enter an integer greater than 0: -1 -1 is not greater than 0 Please enter an integer greater than 0: 0 0 is not greater than 0 Please enter an integer greater than 0: 5 5
~ghoffman/it116_test/ex09.shThe script will prompt you for your Unix username.