You are on page 1of 15

Loops

Dr. Shereef Abu Al-Maati 1


While Loop

while <condition>:
<statement 1>

<statement n>

enter a message: hi shereef


how many times should i print this message: 3
1 , hi shereef
2 , hi shereef
3 , hi shereef
>>>
Dr. Shereef Abu Al-Maati 2
While loop: break

enter a numeric grade: 123


Error: grade must be between 0...100
enter a numeric grade: 5
5
>>>
Dr. Shereef Abu Al-Maati 3
While loop: continue
counter = 0
while counter <= 10:
counter += 1
if counter % 2 == 0:
continue
print(counter)
print('Thank you ')

1
3
5
7
9
11
Thank you
Dr. Shereef Abu Al-Maati 4
While loop :
Same example as before but without break & while True

enter a numeric grade: 567


Error: grade must be between 0...100
enter a numeric grade: 2
2
>>>
Dr. Shereef Abu Al-Maati 5
For Loop
• The Python for statement iterates over the members of a sequence in order, executing the block
each time
for <variable> in <sequence/collection>:
<statement 1>

<statement n>

Dr. Shereef Abu Al-Maati 6


For Loop
• range(stop)
for w in range(3,6): • range(start, stop[, step]) -> range object
print(w,". Hi Shereef!”) • Return an object that produces a sequence
of integers from start (inclusive) to stop
(exclusive) by step.
3 . Hi Shereef!
4 . Hi Shereef!
• Note, it stop at max range -1
5 . Hi Shereef!

Dr. Shereef Abu Al-Maati 7


For Loop
for x in range(3): #Note, it starts at 0 AND goes to max-1
print(x,". Hi Shereef!")

0 . Hi Shereef!
1 . Hi Shereef!
2 . Hi Shereef!

for x in range(3):
print(x,". Hi Shereef!",end=" ")

0 . Hi Shereef! 1 . Hi Shereef! 2 . Hi Shereef!

for x in range(3):
print(x,". Hi Shereef!",end=" , ")

0. Hi Shereef! , 1 . Hi Shereef! , 2 . Hi Shereef! ,

Dr. Shereef Abu Al-Maati 8


For Loop
So
for x in range(3):
print(x,". Hi Shereef!")

Is really equivalent to
for x in range(0,3):
print(x,". Hi Shereef!")

Dr. Shereef Abu Al-Maati 9


range(start, end, increments)

even number between 10-20


10 , 12 , 14 , 16 , 18 , 20 ,
>>>

Dr. Shereef Abu Al-Maati 10


range(start, end, increments)

countdown 10,9,8,..,0
10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ,
>>>

Dr. Shereef Abu Al-Maati 11


Random numbers
The randint function returns a random number
between both ranges inclusive

62126126456623665563
>>>

Dr. Shereef Abu Al-Maati 12


Random numbers --- guessing game
import random
print('Guessing Game, guess the number that the computer selected')
min = int(input('enter the min range, inclusive: '))
max = int(input('enter the max range, inclusive: '))
computerNumber = random.randint(min,max)
times = 0
correctNumber = False
while not correctNumber:
number = int(input('Guess the computer number: '))
times +=1
if number < computerNumber:
print('number too small')
elif number > computerNumber:
print('number too large')
else:
print('Yes correct, in ',times, ' tries')
correctNumber = True
print('Thank you for playing the game')

Dr. Shereef Abu Al-Maati 13


For loop and strings

• String is a sequence of characters,

for count in "Shereef":


print(count)

S
h
e
r
e
e
f

Dr. Shereef Abu Al-Maati 14


For Loop, lists/arrays
for w in [1,3,5,7,9]:
print(w)

1
3
5
7
9

Dr. Shereef Abu Al-Maati 15

You might also like