python3–循环语句

2018-05-31 11:28:49 浏览数 (1)

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

0 人点赞