# python3–循环语句

### if 语句

#### If 条件判断

```if 判断条件：

elif 判断条件：

else：

```#!/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:
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 语句

##### 格式：
```While 判断条件：

break       	跳出循环
continue		跳到下一次循环```
##### 实例：

```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```

#### 无限循环

```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

```while 1:
aa = input("pls input a number: ")
if int(aa) == 222:
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
##### continue

```while 1:
aa = input("pls input a number: ")
if int(aa) == 222:
continue
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 循环的一般格式如下：

```for <variable> in <sequence>:
<statements>
else:
<statements>```
##### 实例1：
```niubi = ["baidu", "google", "tencent", "zhdya"]

for i in niubi:
print("牛逼就是你→{0}".format(i))

##### 实例2：
```niubi = ["baidu", "google", "tencent", "zhdya"]

for i in range(len(niubi)):
print("牛逼就是你→{0}, 世界第{1}强!!".format(niubi[i],i))```

#### 实例3：

##### 九九乘法表
```for i in range(10):
for j in range(1,i+1):
print("{0}*{1}={2}".format(j,i,i*j),end=" ")    //end的作用，在输出的末尾添加不同的字符，这里是用的空格。
print()     //默认这个是换行的意思。```

```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

#### 随堂练：

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

• 利用while语句,条件为输入的字符不为’\n’。
```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）阶乘：

• 判断用户如果是非正常输入：
```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 条评论

• ### Reverse digits of an

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123,...

• ### day37（多进程）- 多进程、守护进程、进程锁

# obj.start() 实际上是用的 run() 方法 # 但是对象不能直接调用 run() 方法 # 直接调用就立即执行，成了单线程 ...

• ### ROC分析时一定要告诉R分析谁

嗨！大家好，我是一棵树，这是我第一次在解螺旋发文，还是蛮激动的。下面就开始吧！ 今天的主题是：ROC分析时一定要告诉R分析谁 用到的软件是：R语言 用到的R包是...

• ### Android笔记：在原生App中嵌入Flutter

Terminal进入到项目根目录，执行flutter create -t module ‘module名字’例如：flutter create -t modul...

### 老七Linux

FCLASSROOM运维开发

FCLASSROOM · 运维开发 (已认证)