DisplayMulti('Hello', 'Cont', 'Goodbye', 'Break', 'Last') and press Enter. Notice
that the words
Cont
and
Break
don’t appear in the output because they’re key-
words. In addition, the word
Last
doesn’t appear in the output because the
for
loop ends before this word is processed.
Using the while statement
The
while
loop statement continues to perform tasks until such time that a con-
dition is no longer true. As with the
for
statement, the
while
statement supports
both the
continue
and
break
keywords for ending the loop prematurely. To see
how this works, open a copy of IPython and type the following code:
def SecretNumber():
GotIt = False
while GotIt == False:
One = int(input("Type a number between 1 and 10: "))
Two = int(input("Type a number between 1 and 10: "))
if (One >= 1) and (One <= 10):
if (Two >= 1) and (Two <= 10):
print('Secret number is: '
+ str(One * Two))
GotIt = True
continue
else:
print("Incorrect second value!")
else:
print("Incorrect first value!")
print("Try again!")
CHAPTER 4
Do'stlaringiz bilan baham: |