Python流程语句:缩进与 if、while、for

本文通过一个Python程序实例介绍了如何使用while和for循环控制语句及条件判断来实现数字猜谜游戏。此外,还展示了break和continue关键字的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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.'

 

转载于:https://my.oschina.net/u/3409834/blog/1549712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值