Professional Documents
Culture Documents
– Polite congratulations
– Stop
Excercise
• A guessing game
– Quit the while loop when the user input the number 11
– Otherwise never stop the game
– If user input correct number, then make new random number
import random
matnum = random.randint(1,9)
guess = eval(input( 'Your guess please(1~10, 11 quit):' ))
while guess != matnum:
if guess > matnum:
print( 'Too high' )
else:
print( 'Too low' )
guess = eval(input( 'Your next guess please: '))
print( 'At last!' )
Excercise
• A guessing game
– Quit the while loop when the user input the number 11
– Otherwise never stop the game
import random
matnum = random.randint(1,9)
guess = eval(input( 'Your guess please(1~10, 11 quit):' ))
while guess != 11:
if guess > matnum:
print( 'Too high' )
elif guess < matnum:
print( 'Too low' )
else:
print( ‘At last!’)
guess = eval(input( 'Your next guess (1~10, 11 quit): '))
while: example
• More than 2 conditions?
• while condition1 and condition2: k=0
while k < 4 and k != 2:
statement k += 1;
print( k )
• while condition1 or condition2:
statement
– True : execute statement
– False : quit the loop
– Condition: <, >, >=, <=, !=, ==
Excercise
• A guessing game
– Quit the while loop if the guess is same as matnum or if the user in-
put the number 11
import random
matnum = random.randint(1,9)
guess = eval(input( 'Your guess please(1~9 , 11 quit):' ))
while guess != matnum:
if guess > matnum:
print( 'Too high' )
else:
print( 'Too low' )
guess = eval(input( 'Your next guess please: '))
print( 'At last!' )
Excercise
• A guessing game
– Quit the while loop if the guess is same as matnum or if the user in-
put the number 11
import random
matnum = random.randint(1,9)
guess = eval(input( 'Your guess please(1~9 , 11 quit):' ))
while guess != matnum and guess != 11:
if guess > matnum:
print( 'Too high' )
else:
print( 'Too low' )
guess = eval(input( 'Your next guess please(1~10 , 11 quit): '))
print( 'At last!' )
break and continue
• If there are a number of different conditions to stop a while loop, you
may be tempted to use a for with the number of repetitions set to some
accepted cut off value (or even inf) but enclosing if statements which
break out of the for when the various conditions are met.
BREAK Terminate execution of WHILE or FOR loop.
BREAK terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.
BREAK is not defined outside of a FOR or WHILE loop.
Use RETURN in this context instead.
import random
matnum = random.randint(1,9)
guess = eval(input( 'Your guess please(1~9 , 11 quit, 12 skip):' ))
while guess != matnum:
if guess > matnum:
print( 'Too high' )
else:
print( 'Too low' )
guess = eval(input( 'Your next guess please: '))
print( 'At last!' )
Excercise
• A guessing game
– Quit the while loop when the user input the number 11 (use break)
– Skip the single repetition when user input the number 12 (use continue)
import random
matnum = random.randint(1,9)guess = eval(input( 'Your guess
please(1~9 , 11 quit, 12 skip):' ))
while guess != matnum:
if guess == 11:
break
if guess == 12:
continue
if guess > matnum:
print( 'Too high' )
else:
print( 'Too low' )
guess = eval(input( 'Your next guess please: '))
print( 'At last!' )
ch3.3,
10, 11, 12, 15, 20, 21, 22, 23, 24, 31
The while Loop
• Executes a block of code repeatedly
• while loop repeatedly executes an in-
dented block of statements
– As long as a certain condition is met
• Form