0°

python常用语句

流程控制if…else….

name = '疯子' 
res = input('你叫什么名字?') 
if res == name: 
    print('帅哥') 
else: 
    print('丑男')

如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨

age_of_girl=18
 height=171 
weight=99 
is_beautiful=True 
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and 
weight < 100 and is_beautiful == True: 
    print('表白...')
else: 
    print('阿姨好')

逻辑运算符


image

小练习:

1.定义布尔型变量 has_ticket 表示是否有车票 #2.定义整形变量 knife_length 表示刀的长度,单位:厘米 #3.首先检查是否有车票,如果有,才允许进行安检 #4.安检时,需要检查棍子的长度,判断是否超过20厘米 # 如果超过20厘米,提示棍子的长度,不允许上车 # 如果不超过20厘米,按键通过 #5.如果没有车票,不允许进门

has_ticket = True
stick_length = 22
if has_ticket: 
    if stick_length > 20: 
        print('你的棍子太长了',stick_length) 
    else: 
        print('请进....') 
else: 
    print('您没有车票,不允许进入车站')

流程控制之while

while 条件: # 循环体

如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
如果条件为假,那么循环体不执行,循环终止

1、循环取出列表里面的每一个元素

 l1 = ['a','b','c','d','e','f']
 count = 0
 while count < len(l1):
     print(l1[count])
     count += 1
0-10之间的偶数
 count = 0
 while count <= 10:
     print(count)
     count += 2

2、死循环

 while True:
     print(1)

3、while….else…..

 count = 0
 while count <= 5 :
     count += 1
     print("Loop",count)
 else:
     print("循环正常执行完啦")

4、continue:跳过本次循环
break:跳出循环
pass:占位符

 count = 0
 while count <= 5:
     count += 1
     if count == 2:
         continue
     print(count)
 count = 0
 while count <= 5:
     count += 1
     if count == 2:
         break
     print(count)
 count = 0
 while count <= 5:
     count += 1
     if count == 2:
         pass
    print(count)

猜拳小练习

import random
print('-----欢迎来猜拳-----')
print('1:石头  2:剪刀  3:布 4:退出')
info = {
'win' :0,
'lose' :0,
'draw' :0
}
while True:
    hum = input('请出拳:').strip()
    com = ['石头', '剪刀', '布']
    com1 = random.choice(com)
    if hum == '1' and com1== '石头' or hum == '2' and com1 == '剪刀' or hum == '3' and com1 == '布':
        print('决战到天亮!')
        info['draw'] += 1
    elif hum == '2' and com1== '石头' or hum == '3' and com1 == '剪刀' or hum == '1' and com1 == '布':
        print('小垃圾!看看我是啥--%s'% com1)
        info['lose'] += 1
    elif hum == '4':
        break
    elif hum == '3' and com1== '石头' or hum == '1' and com1 == '剪刀' or hum == '2' and com1 == '布':
        print('你牛逼!')
        info['win'] += 1
    else:
        print('你倒是出拳啊!渣渣!!')
    print(info)

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!