Python笔记_05_单循环经典题_pass、break、continue_双层循环经典题

本文深入探讨Python中的单循环和双层循环应用,包括经典题目解析,如身高区间判断、打印星号图案、用户登录验证、查找吉利数等,同时讲解pass、break、continue关键字的使用场景。

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

单循环经典题

  1. height

    女生找对象
    男生在1米~1.5米之间 小强 你在哪里?
    男生在1.5~1.7米之间 没有安全感~
    男生 1.7~ 1.8米之间 帅哥 留个电话
    男生 1.8~2米之间 帅哥 你建议多一个女朋友吗

    1.1 python特性写法

    height = float(input("请输入您的身高"))
    if 1 <= height < 1.5:
    	print("小强 你在哪里?")
    
    elif 1.5 <= height <= 1.7:
    	print("没有安全感~")
    
    elif 1.7 < height <= 1.8:
    	print("帅哥 留个电话")
    
    elif 1.8 < height <= 2:
    	print("帅哥 你建议多一个女朋友吗")
    else:
    	print("你输入的值没有该选项")
    

    1.2 通用写法

    height = float(input("请输入您的身高"))
    if 1 <= height and height < 1.5:
    	print("小强 你在哪里?")
    elif 1.5 <= height and height <= 1.7:
    	print("没有安全感~")
    elif 1.7 < height and height <= 1.8:
    	print("帅哥 留个电话")
    elif 1.8 < height and height <= 2:
    	print("帅哥 你建议多一个女朋友吗")
    else:
    	print("你输入的值没有该选项")
    
  2. 用一个变量打印出一行十个小星星

    i = 0
    strvar = ''
    while i<10:
    	#代码逻辑
    	# print("*",end = "")
    	strvar += '*'
    	i+=1
    print(strvar)
    
  3. 打印一行十个小星星 奇数个打印★ 偶数个打印☆

    '''
    0 % 2 = 0
    1 % 2 = 1
    2 % 2 = 0
    3 % 2 = 1
    4 % 2 = 0
    5 % 2 = 1
    ...
    ...
    101 % 2 = 1
    
    
    0 % 3 = 0
    1 % 3 = 1
    2 % 3 = 2
    3 % 3 = 0
    
    0 % 4 = 0
    1 % 4 = 1
    2 % 4 = 2
    3 % 4 = 3
    4 % 4 = 0
    任意数n 和 m进行取余 那么余数范围: 0 ~ (m-1)
    '''
    i= 0
    while i<10:
    	if i % 2 == 0:
    		print("★",end="")
    	else:
    		print("☆",end="")
    	i +=1 
    
  4. 一个循环 打印十行十列小星星

    i = 0 
    while i<100:
    	print("*",end="")
    	
    	# 打印到第十颗星星 换行 print() 
    	if i % 10 == 9:
    		print()
    	
    	i+=1
    
  5. 一个循环 打印十行十列隔列变色小星星

    i = 0 
    while i<100:
    	# 控制输出星星
    	if i % 2 == 0:
    		print("★",end="")
    	else:
    		print("☆",end="")
    	
    	
    	# 打印到第十颗星星 换行 print() 
    	if i % 10 == 9:
    		print()
    	
    	i+=1
    
  6. 一个循环 打印十行十列隔行变色小星星

    '''
    0 // 10 0
    1 // 10 0
    2 // 10 0
    ...
    ...
    9 // 10 0
    0~ 9 和10进行地板除 会产生10个相同0
    
    10 // 10 1
    11 // 10 1
    12 // 10 1
    ...
    ...
    19 // 10 1
    10~ 19 和10进行地板除 会产生10个相同1
    
    ...
    90 // 10 9
    91 // 10 9
    92 // 10 9
    99 // 10 9
    90~ 99 和10进行地板除 会产生10个相同9
    
    任意数n 和 10 进行地板除 会产生10个相同的数字
    0 // 3  = 0
    1 // 3  = 0 
    2 // 3  = 0
    3 // 3 = 1
    任意数n 和 m 进行地板除 会产生m个相同的数字
    
    '''
    i = 0 
    while i<100:
    	# 控制输出星星
    	'''先产生10个相同的数字,在和2进行取余 余数范围只能是0或者1'''
    	if i // 10 % 2 == 0:
    		print("★",end="")
    	else:
    		print("☆",end="")	
    	
    	# 打印到第十颗星星 换行 print() 
    	if i % 10 == 9:
    		print()
    	
    	i+=1
    
  7. 用户登陆(三次输错机会)且每次输错误时显示剩余错误次数

    times = 0 
    while times < 3:
    	pwd = input("请输入您的密码:")
    	if pwd == "000":
    		print("恭喜你,登陆成功")
    		break
    	else:
    		# 剩余次数 = 最大次数- 已经有的次数
    		print("抱歉,密码错误,您还剩下%d次机会" % (2-times))
    		if times == 2:
    			print("老哥,明天再来~")
    	
    	times += 1
    
    

pass、break、continue关键字的使用

pass

过 作用:做站位用的

if 5==5:
	pass

i = 0
while i<10:
	pass  # 约定俗成,在循环里面什么也不写的情况下,给与友好提示;
	i+=1
break

终止当前循环 (只能用在循环当中)

例:打印1~10,如果遇到5终止循环

i= 1
while i<=10:
	if i == 5:
		break
	print(i)
	i+=1

break 终止的是当前循环 , 外面循环一次,里面循环三次,只不过当j=3的时候,循环终止了

i = 1
while i<=3:
	j = 1
	while j<=3:
		if j == 3:
			break
		print(i,j)
		j+=1
	i+=1
continue

跳过当前循环,从下一次循环开始

例:打印1~10 不打印5

i = 1
while i <= 10:
	if i == 5:
		i+=1   # 要小心跳过循环之后,后面的代码不执行了,从循环开始出在执行
		continue
	print(i)
	i+=1

例:打印1~100 所有不含有4的数字

# 第一个方法
i = 1
while i <= 100:
	if i // 10 == 4 or  i % 10 == 4:
		i += 1
		continue
	print(i)
	i += 1

# 第二个方法
i = 1
while i <= 100:
	num = str(i)
	if '4' in num:
		i += 1
		continue
	print(i)
	i += 1

双层循环经典题

  1. 用双层while 写十行十列小星星

    j = 0
    while j < 10:
    
        # 打印一行十个小星星
        i = 0
        while i < 10:
            print("*", end="")
            i += 1
    
        # 打印换行
        print()
    
        j += 1
    
  2. 用双层while 写十行十列隔列换色小星星

    # 变量j 控制的是行
    j = 0
    while j < 10:
    
        # 打印一行十个小星星
        # 变量i控制的是列
        i = 0
        while i < 10:
            if i % 2 == 0:
                print("★", end="")
            else:
                print("☆", end="")
            i += 1
    
        # 打印换行
        print()
    
        j += 1
    
  3. 用双层while 写十行十列隔行换色小星星

    j = 0
    while j < 10:
    
        # 打印一行十个小星星
        # 变量i控制的是列
        i = 0
        while i < 10:
            if j % 2 == 0:
                print("★", end="")
            else:
                print("☆", end="")
            i += 1
    
        # 打印换行
        print()
    
        j += 1
    
    1. 99乘法表
      4.1 方向一:

      # while 循环
      i = 1
      while i <= 9:
      
          # 打印表达式
          j = 1
          while j <= i:
              print("%d*%d=%2d " % (i, j, i * j), end="")
              j += 1
      
          # 打印换行
          print()
          i += 1
      

      4.2 方向二

      # while循环
      i = 1
      while i <= 9:
      
          # 打印空格
          k = 9 - i
          while k > 0:
              print("       ", end="")
              k -= 1
      
          # 打印乘法表达式
          j = 1
          while j <= i:
              print("%d*%d=%2d " % (i, j, i * j), end="")
              j += 1
      
          # 打印换行
          print()
      
          i += 1
      

      4.3 方向三

      # for 循环
      for i in range(9, 0, -1):
          for j in range(1, i + 1):
              print('%d*%d=%2d ' % (i, j, i * j), end='')
          print()
      

      4.4 方向四

      # for 循环
      for i in range(9, 0, -1):
          for k in range(9 - i, 0, -1):
              print('       ', end='')
          for j in range(1, i + 1):
              print('%d*%d=%2d ' % (i, j, i * j), end='')
          print()
      
    2. 100~ 999 找吉利数。111 222 333 … 999 123 321 456 654 …678 876 打印出来

    # while
    # 第一种做法
    i = 100
    while i < 1000:
        gewei = i % 10
        shiwei = i // 10 % 10
        baiwei = i // 100
    
        if shiwei == baiwei == gewei:
            print(i)
        if shiwei == baiwei + 1 and shiwei == gewei - 1:
            print(i)
        if shiwei == baiwei - 1 and shiwei == gewei + 1:
            print(i)
        i += 1
    
    print('=' * 62)  # 分割线
    
    # for
    # 第二种做法
    for i in range(100, 1000):
        strvar = str(i)
        gewei = int(strvar[0])
        shiwei = int(strvar[1])
        baiwei = int(strvar[2])
        if gewei == baiwei and gewei == shiwei:
            print(i)
        if gewei == shiwei - 1 and baiwei == shiwei + 1:
            print(i)
        if gewei == shiwei + 1 and baiwei == shiwei - 1:
            print(i)
    
    1. 百钱买百鸡。

    公鸡1块钱一只 母鸡 3块钱一只 小鸡5毛钱一只 100块钱 买 100只鸡 有多少种买法

    # while
    
    total = 0
    x = 0
    while x <= 100:
        y = 0
        while y <= 33:
            z = 0
            while z <= 100:
                if (x + y + z == 100) and (x * 1 + y * 3 + z * 0.5 == 100):
                    print(x, y, z)
                    total += 1
                z += 1
            y += 1
        x += 1
    print('共有%d种买法' % total)
    
    total = 0
    for x in range(101):
        for y in range(34):
            for z in range(101):
                if (x + y + z == 100) and (x + 3 * y + 0.5 * z == 100):
                    print(x, y, z)
                    total += 1
    print('共有%d种买法' % total)
    
    1. 求1-2+3-4+5-…+99中,除88以外所有数的和:
    # while
    
    i = 1
    total = 0
    while i < 100:
        if i == 88:
            i += 1
            continue
        if i % 2:
            total += i
        else:
            total -= i
        i += 1
    print(total)
    
    # for
    
    total = 0
    for i in range(1, 100):
        if i == 88:
            continue
        if i % 2:
            total += i
        else:
            total -= i
    print(total)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值