執行個體 while+if+break
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
執行個體 while+if+else+break
while True:
s = input('Enter something : ')
if s == 'quit':
break
else:
print('Length of the string is', len(s))
print('Done')
number = 23
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
# 這将導緻 while 循環中止
running = False
elif guess < number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
# 在這裡你可以做你想做的任何事
print('Done')
#continue 語句用以告訴 Python 跳過目前循環塊中的剩餘語句,并繼續該循環的下一次疊代。
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')