Transcript of CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
- Slide 1
- CC0002NI Computer Programming Computer Programming Er. Saroj
Sharan Regmi Week 7
- Slide 2
- Repetition and loops The control structures that implement
repetition, or iteration, are called loops Python has two loop
structures for loop while loop
- Slide 3
- The for loop The for loop is simple to use but is not suitable
for all situations. It can only be used when the number of times
that the loop body is to be executed is fixed at the time of the
loop's execution and this is not always the case. It cannot be
used, for example, when the program needs to prompt a user
repeatedly for input until the appropriate input is received.
- Slide 4
- The while Loop Here is a program that uses a while loop to
prompt the user for a password. It repeatedly prompts until the
correct password is entered. # password.py # Program prompts user
to enter the password # until they do so correctly. print "Entry is
password protected.", password = raw_input("Enter the password! ")
while password != "python": print "Entry is password protected.",
password = raw_input("Enter the password! ") print "Welcome!"
- Slide 5
- The while loop The only lines of code that are new to you in
the above program are those that form the while loop structure:
while password != "python": print "Entry is password protected.",
password = raw_input("Enter the password! ")
- Slide 6
- The while loop The general form of the while loop in Python has
the following syntax: while : When the Python interpreter
encounters a while loop it first evaluates the Boolean expression.
If the Boolean expression evaluates to True, the statements in the
block are executed. If the Boolean expression evaluates to False,
the statements in the block are not executed. Control passes to the
next statement after the block The block can contain one statement
or many statements. All statements in the block must be indented
equally to associate them with the loop
- Slide 7
- The while loop If it still evaluates to True, the statements in
the block are executed again. This process repeats until the
Boolean expression no longer evaluates to True. As soon as it
becomes False, Python passes control to the statement immediately
following the block
- Slide 8
- The while loop # password.py # Program prompts user to enter
the password # until they do so correctly. print "Entry is password
protected.", password = raw_input("Enter the password! ") while
password != "python": print "Entry is password protected.",
password = raw_input("Enter the password! ") print "Welcome!"
- Slide 9
- Infinite loops In the password program once the user initially
enters an incorrect password and the while loop is entered there is
only one way to exit the loop. The correct password had to be
entered. This was no problem for us, since we knew what it was,
python. But suppose we didn't the loop would not terminate it would
become an infinite loop.
- Slide 10
- Infinite loops In the password program once the user initially
enters an incorrect password and the while loop is entered there is
only one way to exit the loop. The correct password had to be
entered. This was no problem for us, since we knew what it was,
python. But suppose we didn't the loop would not terminate it would
become an infinite loop. The password program can be made more
practical by limiting the number of attempts that the user is
allowed to input the correct password. This will be done as an
exercise later in the chapter.
- Slide 11
- Input Validation The next program is logically very similar to
the previous one. The while loop is again used to repeatedly prompt
the user for integer input this time until the input is of the
appropriate size. Open the program validate.py in IDLE and Run it.
The program requires the input integer to be between 5 and 10
inclusive (i.e. 5, 6, 7, 8, 9 or 10)
- Slide 12
- Input Validation # validate.py # Program prompts user to enter
an integer between 5 and # 10 (inclusive) until they do so
correctly. number = input("Enter an integer between 5 and 10
(inclusive): ") while number 10: print "Invalid input!", number =
input("Enter an integer between 5 and 10 (inclusive): ") print
"Thank-you!"
- Slide 13
- The Boolean Condition The Boolean condition this time is:
number 10 We need a Boolean condition that will test True when
user's input is inappropriate. Correct input is between 5 and 10
inclusive. Inappropriate input would be less than 5 or bigger than
10.
- Slide 14
- while VS for Loop In this chapter you will see conditional
loops being used in a variety of programs. Most of these programs
could not have been written using for loops. This is because the
number of iterations of the loop that would be required was not
fixed in advance of the loop's execution
- Slide 15
- while VS for Loop The reverse is not the case. Any program that
can be written using a for loop can also be written using a while
loop. As you will see, for loops are generally easier to write so
we strongly recommend that you always a for loop where
possible
- Slide 16
- 2 Times table Using for loopUsing while loop # twoTimesTable.py
# prints out the two times table #from 1 to 10 # using a for loop
print "Two times table " for i in range (1,11): result = i * 2
print i, "x 2 =", result # twoTimesTable2.py # prints out the two
times table #from 1 to 10 using # a while loop print "Two times
table " i = 1 while i