基础班-第04天{if、while、for}

2018-11-05  本文已影响0人  无罪的坏人

判断语句和循环语句

if-else

想⼀想:在使⽤if的时候,它只能做到满⾜条件时要做的事情。那万⼀ 需要在不满⾜条件的时候,做某些事,该怎么办呢?
答:else

<1>if-else的使⽤格式

if条件:
      满⾜条件时要做的事情1
      满⾜条件时要做的事情2
      满⾜条件时要做的事情3
      ...(省略)...
else:
      不满⾜条件时要做的事情1
      不满⾜条件时要做的事情2
      不满⾜条件时要做的事情3
      ...(省略)...

demo1

chePiao = 1 #⽤1代表有⻋票,0代表没有⻋票                
if chePiao == 1:
    print("有⻋票,可以上⽕⻋")                              
    print("终于可以⻅到Ta了,美滋滋~~~")
else:                               
    print("没有⻋票,不能上⻋")                              
    print("亲爱的,那就下次⻅了,⼀票难求啊~~~~(>_<)~~~~")

结果:有车票的情况

有⻋票,可以上⽕⻋
终于可以⻅到Ta了,美滋滋~~~

结果2:没有⻋票的情况

没有⻋票,不能上课
亲爱的,那就下次⻅了,⼀票难求啊~~~(>_<)~~~

<2>练⼀练

要求:从键盘输⼊⼑⼦的⻓度,如果⼑⼦⻓度没有超过10cm,则允许上⽕ ⻋,否则不允许上⽕⻋


elif

if能完成当xxx时做事情
if-else能完成当xxx时做事情1,否则做事情2
如果有这样⼀种情况:当xxx1时做事情1,当xxx2时做事情2,当 xxx3时做事情3,那该怎么实现呢?

elif

<1>elif的功能

elif的使⽤格式如下:

if xxx1:
      事情1
elif xxx2:
      事情2
elif xxx3:
      事情3

说明:

demo:

score = 77
if score>=90 and score<=100:                                
        print('本次考试,等级为A')              
elif    score>=80   and score<90:                               
        print('本次考试,等级为B')              
elif    score>=70   and score<80:                               
        print('本次考试,等级为C')              
elif    score>=60   and score<70:                               
        print('本次考试,等级为D')              
elif    score>=0    and score<60:                               
        print('本次考试,等级为E')

<2>注意点

if 性别为男性:
      输出男性的特征
      ...

elif 性别为⼥性:
      输出⼥性的特征

      ...
else:
      第三种性别的特征
      ...

说明:


if嵌套

通过学习if的基本⽤法,已经知道了

坐⽕⻋或者地铁的实际情况是:先进⾏安检如果安检通过才会判断是否有⻋票,或者是先检查是否有⻋票之后才会进⾏安检,即实际的情况某 个判断是再另外⼀个判断成⽴的基础上进⾏的,这样的情况该怎样解决呢?

答:

if嵌套

<1>if嵌套的格式

if 条件1:
      满⾜条件1 做的事情1
      满⾜条件1 做的事情2
      ...(省略)...
      if 条件2:
            满⾜条件2 做的事情1
            满⾜条件2 做的事情2

<2>if嵌套的应⽤

demo:

chePiao = 1 # ⽤1代表有⻋票,0代表没有⻋票               
daoLenght = 9     # ⼑⼦的⻓度,单位为cm

if chePiao == 1:
    print("有⻋票,可以进站")                               
    if  daoLenght < 10:                                             
        print("通过安检")                                               
        print("终于可以⻅到Ta了,美滋滋~~~")
    else:                                               
        print("没有通过安检")
        print("⼑⼦的⻓度超过规定,等待警察处理...")                
else:                               
    print("没有⻋票,不能进站")
    print("亲爱的,那就下次⻅了,⼀票难求啊~~~~(>_<)~~~~")

结果1:chePiao = 1;daoLenght = 9

有⻋票,可以进站
通过安检
终于可以⻅到Ta了,美滋滋~~~

结果2:chePiao = 1;daoLenght = 20

有⻋票,可以进站
没有通过安检
⼑⼦的⻓度超过规定,等待警察处理...

结果3:chePiao = 0;daoLenght = 9

没有⻋票,不能进站
亲爱的,那就下次⻅了,⼀票难求啊~~~(>_<)~~~

结果4:chePiao = 0;daoLenght = 20

没有⻋票,不能进站
亲爱的,那就下次⻅了,⼀票难求啊~~~(>_<)~~~

想⼀想:为什么结果3和结果4相同???

<3>练⼀练

情节描述:上公交⻋,并且可以有座位坐下
要求:输⼊公交卡当前的余额,只要超过2元,就可以上公交⻋;如果空座位 的数量⼤于0,就可以坐下

应⽤:猜拳游戏

<1>运⾏效果:

image.png

<2>参考代码:

import random
player = input('请输⼊:剪⼑(0) ⽯头(1) 布(2):')
player = int(player)
computer = random.randint(0,2)
# ⽤来进⾏测试
#print('player=%d,computer=%d',(player,computer))
if ((player == 0) and (computer == 2)) or ((player ==1) and (computer ==
    print('获胜,哈哈,你太厉害了')
elif player == computer:
    print('平局,要不再来⼀局')
else:
    print('输了,不要⾛,洗洗⼿接着来,决战到天亮')

循环介绍

<1>⽣活中的循环场景

跑道


image.png

⻛扇


image.png
CF加特林
image.png

<2>软件开发中循环的使⽤场景

跟媳妇承认错误,说⼀万遍"媳妇⼉,我错了"

print("媳妇⼉,我错了")
print("媳妇⼉,我错了")
print("媳妇⼉,我错了")
...(还有99997遍)...

使⽤循环语句⼀句话搞定

i = 0
while i<10000:
print("媳妇⼉,我错了")
    i+=1

<3>⼩总结

while循环

<1>while循环的格式

while 条件:
      条件满⾜时,做的事情1
      条件满⾜时,做的事情2
      条件满⾜时,做的事情3
      ...(省略)...

demo

i = 0
while i<5:
    print("当前是第%d次执⾏循环"%(i+1))
    print("i=%d"%i)
    i+=1

结果:

当前是第1次执⾏循环
i=0
当前是第2次执⾏循环
i=1
当前是第3次执⾏循环
i=2
当前是第4次执⾏循环
i=3
当前是第5次执⾏循环
i=4

while循环应⽤

1. 计算1~100的累积和(包含1和100)

参考代码如下:

#encoding=utf-8
i = 1
sum = 0
while i<=100:
    sum = sum + i
    i += 1
print("1~100的累积和为:%d"%sum)

2. 计算1~100之间偶数的累积和(包含1和100)

参考代码如下:

#encoding=utf-8
i = 1
sum = 0
while i<=100:
    if i%2 == 0:
        sum = sum + i
    i+=1
print("1~100的累积和为:%d"%sum)

while循环嵌套

<1>while嵌套的格式

while 条件1:
      条件1满⾜时,做的事情1
      条件1满⾜时,做的事情2
      条件1满⾜时,做的事情3
      ...(省略)...
      while 条件2:
            条件2满⾜时,做的事情1
            条件2满⾜时,做的事情2
            条件2满⾜时,做的事情3
            ...(省略)...

<2>while嵌套应⽤⼀

要求:打印如下图形:

*
* *
* * *
* * * *
* * * * *

参考代码:

i = 1
while i<=5:
    j = 1
    while j<=i:
        print("* ",end='')
        j+=1
    print("\n")
    i+=1

<3>while嵌套应⽤⼆:九九乘法表

image.png

参考代码:

i = 1
while i<=9:
    j=1
    while j<=i:
        print("%d*%d=%-2d "%(j,i,i*j),end='')
        j+=1
    print('\n')
    i+=1

for循环

像while循环⼀样,for可以完成循环的功能。
在Python中 for循环可以遍历任何序列的项⽬,如⼀个列表或者⼀个字符串
等。

for循环的格式

for 临时变量 in 列表或者字符串等:
      循环满⾜条件时执⾏的代码
else:
      循环不满⾜条件时执⾏的代码

demo1

name = 'dongGe'
for x in name:
    print(x)

运⾏结果如下:


image.png

demo2

name = ''
for x in name:
    print(x)
else:
    print("没有数据")

运⾏结果如下:


image.png

break和continue

1. break

<1> for循环

name = 'dongGe'
for x in name:
    print('----')
    print(x)

执行结果:

----
d
----
o
----
n
----
g
----
G
----
e
name = 'dongGe'
for x in name:
    print('----')
    if x == 'g':
        break
    print(x)

运行结果:

----
d
----
o
----
n

<2> while循环

i = 0
while i<10:
    i = i+1
    print('----')
    print(i)

执行结果:

----
1
----
2
----
3
----
4
----
5
----
6
----
7
----
8
----
9
----
10
i = 0
while i<10:
    i = i+1
    print('----')
    if i==5:
        break
    print(i)

运行结果:

----
1
----
2
----
3
----
4
----

小总结:

2. continue

<1> for循环

name = 'dongGe'
for x in name:
    print('----')
    if x == 'g':
        continue
    print(x)

运行结果:

----
d
----
o
----
n
----
----
G
----
e

<2> while循环

i = 0
while i<10:
    i = i+1
    print('----')
    if i==5:
        continue
    print(i)

运行结果:

----
1
----
2
----
3
----
4
----
----
6
----
7
----
8
----
9
----
10

小总结:

3. 注意点

总结

1️⃣基本方法

if 条件:
      满⾜时做的事情

2️⃣满⾜与否执⾏不同的事情

if 条件:
      满⾜时做的事情
else:
      不满⾜时做的事情

3️⃣多个条件的判断

if 条件:
      满⾜时做的事情
elif 条件2:
      满⾜条件2时做的事情
elif 条件3:
      满⾜条件3时做的事情
else:
      条件都不满⾜时做的事情

4️⃣嵌套

if 条件:
      满⾜时做的事情
      这⾥还可以放⼊其他任何形式的if判断语句

i = 0
while i<10:
    print("hello")
    i+=1
name = 'dongGe'
for x in name:
    print(x)

作业

1. 使⽤if,编写程序,实现以下功能:

username = input("请输入用户名:")
password = input("请输入密码:")

if username == 'root' and password == '123456':
    print("欢迎进⼊xxx的世界")
else:
    print("用户名或密码错误")

2. 使⽤while,完成以下图形的输出

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
i = 1
while i <= 5:
    j = 1
    while j<=i:
        print("* ", end='')
        j += 1
    print("\n")
    i += 1

m = 1
while m < 5:
    n = 1
    while n < 5:
        if n >= m:
            print("* ", end='')
        n += 1
    print("\n")
    m += 1

倒三角形的打印可以用for循环

for i in range(1, 5):
      for j in range(1, 5):
          if j>=i:
              print("* ", end='')

      print("\n")
上一篇 下一篇

猜你喜欢

热点阅读