Python流程語句:縮進與 if、while、for

  • if[條件] … elif[條件] … else 條件控制語句:執行相應的條件語句。
  • while [條件] … else 循環控制語句(無邊界):當條件爲真時,則執行;條件爲假,則不執行。
  • for [條件] … else 循環控制語句(有邊界):同While。
  • break/continue :用於循環控制語句中。break爲跳出當前循環,並結束循環;continue爲從新進入循環條件判斷。
#!/usr/bin/env python
# Filename: expression
 
running = True
number = int(input('Enter an integer: '))
again = 0
 
while running:
	if number == area:
		print 'Congratulations,you number it.' #New block starts here.
		print "(but you do not win any prizes!)" #New block ends here.
		running = False # this cause the while loop to stop
	elif number < area:
		print 'No, it is a little higher than that.' #Another block.
		# You can do whatever you want in a block ...
		number = number + 1
		again = again + 1
		print 'while running again, this',again,'agains.'
	else:
		print 'NO, it is a little lower than that.'
		# You must have number > aera to reach here.
		number = number - 1
		again = again + 1
		print 'while running again, this',again,'agains.'
else:
	print 'The while loop is over.'
	# Do anything else you want to do here .
 
pt = 0
fu = 0
# range(a,b,c).
# a is first print number .
# b is max number , but print number  not eq b .
# c is the print number length . 
# eg: print number = a + c , and a =< print number < b .
 
# break and continue for loop.
for i in range(0,again,1):
	pt = pt +1
	fu = fu + 2
	print 'The' , pt , "print number of 'for loop' is" , i ,'.'
	if pt > 3 :
		print "The loop of for will end to break stop."
		break
	elif pt == 3 :
		print "The loop of for will end last for again."
		continue
else:
	print 'The loop of for will end to normal.'
相關文章
相關標籤/搜索