zoukankan      html  css  js  c++  java
  • Python基础-----while循环练习

    一、使用while循环输出1 2 3 4 5 6   8 9 10

     1 #!/usr/bin/env python 3
     2 # -*- coding:utf-8 -*-
     3 
     4 '''
     5 使用while循环输出1 2 3 4 5 6   8 9 10
     6 '''
     7 
     8 count = 1
     9 while count<=10:
    10     if count == 7:
    11         pass
    12     else:
    13         print(count)
    14     count += 1

    二、使用while循环输出1~100的和

     1 #!/usr/bin/env python 3
     2 # -*- coding:utf-8 -*-
     3 
     4 '''
     5 使用while循环输出1~100的和
     6 '''
     7 
     8 i = 1
     9 sum = 0
    10 while i<=100:
    11     sum = sum + i
    12     i += 1
    13 print(sum)

    三、使用while循环输出1~100内所有的奇数

    #!/usr/bin/env python 3
    # -*- coding:utf-8 -*-
    
    '''
    使用while循环输出1~100内所有的奇数
    '''
    i = 0
    while i < 100:
        i+=1
        while i % 2 != 0:
            print(i)
            i += 1

    四、使用while循环输出1~100内所有的偶数

    #!/usr/bin/env python 3
    # -*- coding:utf-8 -*-
    
    '''
    使用while循环输出1~100内所有的偶数
    '''
    i = 0
    while i < 100:
        i+=1
        while i % 2 == 0:
            print(i)
            i += 1

    五、使用while循环输出1-2+3-4+5.....+99的和

     1 #!/usr/bin/env python 3
     2 # -*- coding:utf-8 -*-
     3 
     4 '''
     5 使用while循环输出1-2+3-4+5.....+99的和
     6 '''
     7 
     8 i = 1
     9 sum = 0
    10 while i<=99:
    11     if i % 2 == 0:
    12         sum = sum - i
    13     else:
    14         sum = sum + i
    15     i += 1
    16 print(sum)

    六、判断用户登录,三次机会重试

     1 #!/usr/bin/env python 3
     2 # -*- coding:utf-8 -*-
     3 
     4 '''
     5 用户登录,三次机会重试
     6 '''
     7 
     8 
     9 count = 1
    10 while count <= 3:
    11 
    12     if input('请输入用户名:') == 'Liming':
    13         print('登录成功!')
    14         break
    15     else:
    16         print('请重试!')
    17     count += 1
    18 else:
    19     print('已失败三次,今日无法登录。')
  • 相关阅读:
    银行数仓主题划份
    Halcon 图像的算术运算(crop_part,invert_image,scale_image)
    Halcon 图像截取 crop_part
    Halcon 算子 threshold
    Halcon 图片读取以及图像转换
    Lens shading correction 的四种方法
    Micro-Manage/ImageJ软件使用技巧快问快答
    Micro-Manager基本操作指南(下)
    Micro-Manager基本操作指南(上)
    MATLAB 配置 Micro-Manager
  • 原文地址:https://www.cnblogs.com/Meanwey/p/9497046.html
Copyright © 2011-2022 走看看