前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3–循环语句

python3–循环语句

作者头像
老七Linux
发布2018-05-31 11:28:49
5910
发布2018-05-31 11:28:49
举报
文章被收录于专栏:Laoqi's Linux运维专列

if 语句

缩进格式

冒号

If 条件判断
代码语言:javascript
复制
if 判断条件:
执行语句
elif 判断条件:
执行语句
else:
执行语句

简单的一个判断学生成绩的例子:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 4/3/2018 8:27 PM
# @Author  : zhdya
# @File    : demon1.py

number = input("pls input your score: ")

if int(number) >= 90 and int(number) <= 100:
    print("your score is {0} very good!".format(number))
elif int(number) >= 80 and int(number) < 90:
    print("your score is {0} good!!".format(number))
elif int(number) >= 60 and int(number) < 80:
    print("your score is {0}, you should work hard!!".format(number))
elif int(number) <= 60:
    print("you socre is {0}, not good!!".format(number))
else:
    print("you input {0} was wrong!!!".format(number))

while 语句

格式:
代码语言:javascript
复制
While 判断条件:
执行语句

break       	跳出循环
continue		跳到下一次循环
实例:

计算1-100的和:

代码语言:javascript
复制
aa = 1
sum = 0

while aa <= 100:
    sum += aa
    aa += 1
print("the sum of 100 is {0}".format(sum))

输出:
the sum of 100 is 5050
无限循环

我们可以通过设置条件表达式永远不为 false 来实现无限循环,实例如下:

代码语言:javascript
复制
while 1:
    aa = input("pls input a number: ")
    int(aa)
    print("the number you input is {0}".format(aa))
    
输出:
pls input a number: 88
the number you input is 88
pls input a number: 86
the number you input is 86
pls input a number: 321
the number you input is 321
break

直接跳出整个循环体。

代码语言:javascript
复制
while 1:
    aa = input("pls input a number: ")
    if int(aa) == 222:
        print("you already input end command!")
        break
    else:
        print("the number you input is {0}".format(aa))

输出:
pls input a number: 55
the number you input is 55
pls input a number: 25
the number you input is 25
pls input a number: 222
you already input end command!
continue

直接跳出本次循环,继续执行下次循环。

代码语言:javascript
复制
while 1:
    aa = input("pls input a number: ")
    if int(aa) == 222:
        continue
        print("you already input end command!")
    else:
        print("the number you input is {0}".format(aa))

输出:
pls input a number: 23
the number you input is 23
pls input a number: 222     //直接跳出本次循环,没有执行下面的print语句。
pls input a number: 333
the number you input is 333

for 语句

for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for 循环的一般格式如下:

代码语言:javascript
复制
for <variable> in <sequence>:
    <statements>
else:
    <statements>
实例1:
代码语言:javascript
复制
niubi = ["baidu", "google", "tencent", "zhdya"]

for i in niubi:
    print("牛逼就是你→{0}".format(i))
    
输出:
牛逼就是你→baidu
牛逼就是你→google
牛逼就是你→tencent
牛逼就是你→zhdya
实例2:
代码语言:javascript
复制
niubi = ["baidu", "google", "tencent", "zhdya"]

for i in range(len(niubi)):
    print("牛逼就是你→{0}, 世界第{1}强!!".format(niubi[i],i))
实例3:
九九乘法表
代码语言:javascript
复制
for i in range(10):
    for j in range(1,i+1):
        print("{0}*{1}={2}".format(j,i,i*j),end=" ")    //end的作用,在输出的末尾添加不同的字符,这里是用的空格。
    print()     //默认这个是换行的意思。

解析:

代码语言:javascript
复制
1*1=1           i=1 j=1
1*2=2 2*2=4     i=1 j=2
1*3=3 2*3=6 3*3=9       i=1 j=3

分析出来 i的值最小为1 j的值最大为行号。
随堂练:

(1)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

  • 利用while语句,条件为输入的字符不为’\n’。
代码语言:javascript
复制
i_str = 0
sum = 0
i_num = 0
i_spa = 0
i_spe = 0

aa = input("pls input sth you like: ")

while aa == "\\n":
    break
else :

    for i in aa:
        if i.isalpha():
            i_str += 1
        elif i.isdigit():
            i_num += 1
        elif i.isspace():
            i_spa += 1
        else :
            i_spe += 1
    print("the number of charcter is %d" %i_str)
    print("the number of number is %d" %i_num)
    print("the number of space is %d" %i_spa)
    print("the number of special worlds is %d" %i_spe)

for i in range(10):
    for j in range(1, i+1):
        print("%d * %d = %d" %(j,i,j*i),end=" ")
    print(" ")
    
输出:
pls input sth you like: 345 %^&*  FGHJs
the number of charcter is 5
the number of number is 3
the number of space is 3
the number of special worlds is 4

(2)阶乘:

随便输入一个数字,得出这个数字的阶乘

  • 判断用户如果是非正常输入:
代码语言:javascript
复制
sum = 1
while 1:
    number = input("pls input a number: ")
    if number.isdigit():
        for i in range(1, int(number)+1):
            sum *= i
        print("the {0} factorial is {1}".format(number, sum))
        break
    elif number.isalpha():
        print("pls just input a number!!")
    else:
        print("pls just input a number!!")

输出:
pls input a number: asd
pls just input a number!!
pls input a number:    
pls just input a number!!
pls input a number: %^&*
pls just input a number!!
pls input a number: asd213e
pls just input a number!!
pls input a number: 5
the 5 factorial is 120
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/07/08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • if 语句
    • 缩进格式
      • If 条件判断
        • 格式:
        • 实例:
    • while 语句
      • 无限循环
        • break
        • continue
        • 实例1:
        • 实例2:
    • for 语句
      • 实例3:
        • 九九乘法表
      • 随堂练:
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档