if进阶:
table shift+table
if的嵌套:
if has_ticket:
print(‘车票检车通过,准备开始安检’)
if knife_length>20:
print(‘你携带的刀太长了’,‘有%d公分长’ % knife_length)
print(‘不允许上车’)
else:
print(‘安检已通过,祝你旅途愉快’)
else:
print(‘大哥,请先买票’)
random.randint(a,b) #a,b之间的数
石头剪刀布:
player=int(input('请输入您要出的拳 石头(1)/剪刀(2)/布(3): '))
computer=3
print(‘玩家选择的拳头是 %d -电脑出的拳是 %d’ % (player,computer))
if ((player1 and computer2)
or (player2 and computer3)
or (player3 and computer1)):
print('你赢了,电脑弱爆了')
elif player == computer:
print(‘真是心有灵犀啊,再来一盘’)
else:
print(‘你输了,弱爆了’)
---------------------------------------------------------
import random
player=int(input('请输入您要出的拳 石头(1)/剪刀(2)/布(3): '))
computer=random.randint(1,3)
print(‘玩家选择的拳头是 %d -电脑出的拳是 %d’ % (player,computer))
if ((player1 and computer2)
or (player2 and computer3)
or (player3 and computer1)):
print('你赢了,电脑弱爆了')
elif player == computer:
print(‘真是心有灵犀啊,再来一盘’)
else:
print(‘你输了,弱爆了’)
循环: while 循环
break continue
顺序–》 从上至下,顺序执行代码
分支–》根据条件判断,决定执行代码的分支
循环–》让特定代码重复执行
初始条件设置—通常是重复执行的计数器
while 条件(判断 计数器 是否达到 目标次数):
条件满足时,做的事情1
条件满足时,做的事情2
条件满足时,做的事情3
处理条件(计数器+1)
i=0
while i<=3:
print(‘Hello world’)
i+=1
print(‘循环结束后,i=%d’ % i)
计数器加上就不会死循环!!
0-100的求和:
result=0
i=0
while i<=100:
print(i)
result+=i
i+=1
print(i)
print(result)
ctrl+x 删除一行
result = 0
i=0
while i <= 100:
#偶数时,if i % 2 == 0
if i % 2!= 0:
print(i)
result+=i
i+=1
print(‘0-100之间的偶数和是result=%d’ % result)
i=0
while i <10:
if i == 3:
break
print(i)
i+=1
print(‘over’)
#当满足某一个条件时,结束
i=0
while i <10:
if i == 3:
i+=1
continue
只有某个条件符合时,继续执行
print(i)
i+=1
print(‘over’)
row=1
while row <=5:
print(’*’ * row)
row+=1