Lecture 12 Loop Structures PartII
Lecture 12 Loop Structures PartII
languages
Loop Structures- Part II
Lecture 12
Today…
• Last Session:
• Loop Structures- Part II
• Today’s Session:
• Loop Structures- Part III:
• While Statement
• Nested Loops
• Various Examples
Recap: Fibonacci Sequence
• Suppose we want to write a program that computes and outputs the
nth Fibonacci number, where n is a value entered by a user
while <condition>:
<body>
Is <condition> No
True?
Yes
<body>
Example
• Count = 0
• While count<9:
• print(“number:”, count)
• count = count+1
• Print(“good bye”)
A little guessing game
• This little code shows code for a
guessing game
• The random number is
generated from 0- 20 and once
that number is generated you
need to guess that number
• In this code we don’t know the
number of iterations we need
to have in order to guess the
answer therefore the reason
we have used the while loop
Revisiting Average of a Series of Numbers
• Here is how we have done it before:
def main():
n = eval(input("How many numbers do you have? "))
sum = 0.0
for i in range(n):
x = eval(input("Enter a number >> "))
sum = sum + x
main()
Revisiting Average of a Series of Numbers
• Here is how we can do it now using a while statement:
sum = 0.0
n = eval(input("How many numbers do you have? "))
count = 0
while count < n:
x = eval(input("Enter a number >> "))
sum = sum + x
count = count + 1
print("The average of the " + str(n) + " numbers you entered is ", sum/n)
Revisiting Average of a Series of Numbers
• Here is also another version that assumes no prior knowledge about
the quantity of numbers the user will input
sum = 0.0
count = 0
moreData = "yes"
while moreData == "yes":
x = eval(input("Enter a number >> "))
sum = sum + x
count = count + 1
moreData = input("Do you have more numbers (yes or no)? ")
print("The average of the " + str(count) + " numbers you entered is ", sum/count)
Printing Odd Numbers With Input Validation
• Suppose we want to print the odd numbers between two user-input
numbers (inclusive), say, start and end
• The program assumes some conditions, whereby the start and end
numbers shall be positive and end should be always greater than start
• Hence, we should continue prompting the user for the correct input
before proceeding with printing the odd numbers
• This process is typically called input validation
• Well-engineered programs should validate inputs whenever possible!
Printing Odd Numbers With Input Validation
1. while True:
2. start = eval(input("Enter start number: "))
3. end = eval(input("Enter end number: "))
4. if start >=0 and end >= 0 and end > start:
5. break It breaks the loop; execution continues at line 8.
6. else:
7. print("Please enter positive numbers, with end being greater than start")
8.
9. for i in range(start, end + 1):
10. if i % 2 == 0:
11. continue It skips one iteration in the loop; execution
12. print(i, end = " ") continues back at line 9.
Nested Loops
• Like the if statement, loops can also be nested to produce
sophisticated algorithms
• Example: Write a program that prints the following rhombus shape
*
* *
* *
* *
* *
* *
* *
* *
*
The Rhombus Example
• One way (not necessarily the best way!) to think about this problem is to
assume that the stars are within a matrix with equal rows and columns
1 2 3 4 5 6 7 8 9
1 *
2 * *
3 * *
Can you figure
4 * *
5 * * out the different
6 * * relationships
7 * * between rows
8 * *
and columns?
9 *
The Rhombus Example
• One way (not necessarily the best way!) to think about this problem is to
assume that the stars are within a matrix with equal rows and columns
1 2 3 4 5 6 7 8 9
1 *
2 * *
3 * * Print a star when:
4 * * 1) Row + Column == 6
5 * *
2) Row + Column == 14
6 * *
7 * * 3) Row – Column == 4
8 * * 4) Column – Row == 4
9 *
The Rhombus Example
• Here is one way of writing the program in Python
for i in range(1, 10):
for j in range(1, 10):
if ((i+j== 6) or (j-i==4) or (i+j == 14) or (i-j==4)):
print("*", end = "")
else:
print(" ", end = "")
print()
rows = abs(rows)
columns = abs(columns)
The Rhombus Example: A More General Version
for i in range(1, rows+1):
for j in range(1, columns+1):
if ((i+j== (columns//2 +2)) or (j-i==(columns//2)) or (i+j ==
(columns+ math.ceil(rows/2))) or (i-j==(columns//2))):
print("*", end = "")
else:
print(" ", end = "")
print()
Next Lecture…
• Problem Solving