1 2 3 4

python流程控制语句

if

if

语法

if 表达式(条件):
	条件成立执行的语句

注意缩进,用tab键或者4个空格,但是尽量不要混用,推荐使用tab键

下面这些情况不会执行后面的打印语句

a = None
b = ''
c = 0
d = False

if a:
	print('a')
if b:
	print('b')
if c:
	print('c')
if d:
	print('d')

and两边都为真,才会执行打印hello world

username = input('用户名: ')
age = int(input('年龄: '))

if username and age>18:
	print('hello world')

print('----game over----')
用户名: admin
年龄: 17
----game over----

if else

语法

if 表达式(条件):
	条件成立执行的语句
else:
	条件不成立执行的语句
print('*'*10,'欢迎来到消消乐','*'*10)
level = input('请输入你的级别(lv1,lv2): ')
if level=='1v1':
	print('免费玩 随便玩')
else:
	print('已经进入付费级别,充值继续玩')
	money = int(input('请充值(必须是10的倍数): '))
	if money%10==0 and money>0:
		print('充值成功,充值金额:',money)
	else:
		print('充值失败,充值金额必须是10的倍数')
********** 欢迎来到消消乐 **********
请输入你的级别(lv1,lv2): lv2
已经进入付费级别,充值继续玩
请充值(必须是10的倍数): 0
充值失败,充值金额必须是10的倍数

猜数字小游戏

思路步骤

1, 系统产生一个随机数
2, 键盘输入一个数
3, 将系统产生的与键盘输入的进行比较
4, 猜对了,中大奖. 猜错了,下次继续

import random

print('*'*10,'数字范围1-10','*'*10)
ran = random.randint(1,10)
num1 = int(input('请输入您要猜的数字: '))

if ran == num1:
	print('恭喜猜对了')
else:
	print('猜错了')
********** 数字范围1-10 **********
请输入您要猜的数字: 1
猜错了

if elif else

多层条件判断
语法

if 条件1:
	条件1成立时执行的语句
elif 条件2:
	条件2成立时执行的语句
elif 条件3:
...
else:
	上述条件都不成立执行的语句

例:成绩判断

fraction = int(input('请输入成绩,进行评级: '))
if fraction >= 90 and fraction <=100:
	print('优+')
elif fraction >= 80 and fraction < 90:
	print('优-')
elif fraction >= 70 and fraction < 80:
	print('良')
elif fraction >= 60 and fraction < 70:
	print('及格')
elif fraction < 60 and fraction >=0:
	print('不及格')
else:
	print('输入错误,请重试')
请输入成绩,进行评级: 89
优-

例:猜宋宋姐年龄

age = int(input('请猜猜宋宋姐年龄: '))

if age <= 18 and age > 0:
	print('(o゜▽゜)o☆[BINGO!] 太有眼光啦! ')
elif age > 18 and age < 30:
	print('人家还是宝宝呢...')
elif age > 30 and age < 40:
	print('人家还很年轻...')
else:
	print('输入错误!!!!!')
请猜猜宋宋姐年龄: 18
(o゜▽゜)o☆[BINGO!] 太有眼光啦!
请猜猜宋宋姐年龄: 40
输入错误!!!!!

for

语法

for 变量名 in 集合:
	语句
for i in range(1,6):
	print('吃了{}个馒头'.format(i))

print('终于吃饱了')
吃了1个馒头
吃了2个馒头
吃了3个馒头
吃了4个馒头
吃了5个馒头
终于吃饱了

range(1,6)函数,等同于range(6)
包含1,不包含6

for else

for和else配合使用(python中独有)
else:适用于for执行完或者没有循环数据时,需要做的事情

name = 'tom'
num = int(input('请输入需要的馒头数量: '))

for i in range(num):
	print('{}很饿,正在吃第{}个馒头'.format(name,i+1))
else:
	print('{}饿哭了'.format(name))
请输入需要的馒头数量: 2
tom很饿,正在吃第1个馒头
tom很饿,正在吃第2个馒头
tom饿哭了

pass

作用:占位,保证语法正确性

if True:
	pass
else:
	pass

continue

退出本次循环进入下一次循环

for i in range(10):
	if i == 4:
		continue
	print(i,end=' ')
0 1 2 3 5 6 7 8 9

break

跳出整个for循环

用户的账号密码登录,只能登录三次,如果三次未成功账户锁定

for i in range(3):
	username = input('localhost login: ')
	password = input('Password: ')
	if username == 'root' and password == '123456':
		print('[root@VM_0_7_centos ~]# ')
		break
	else:
		print('Login incorrect')
else:
	print('账号被锁定,需要重新激活')
localhost login: root
Password: 1
Login incorrect
localhost login: root
Password: 1
Login incorrect
localhost login: root
Password: 1
Login incorrect
账号被锁定,需要重新激活
localhost login: root
Password: 123456
[root@VM_0_7_centos ~]#

循环嵌套:乘法表

for i in range(1,10):
	for j in range(1,10):
		if i >= j:
			print(j,'*',i,'=',i*j,end=' ',sep='')
	print()
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

while

语法

while 条件:
	语句块

打印1-10之间的数字

i = 0
while i <= 10:
	print(i)
	i += 1
print('打印完毕')
0
1
2
3
4
5
6
7
8
9
10
打印完毕

打印1~30之间所有3的倍数

n = 1
while n <= 30:
	if n%3 == 0:
		print(n)
	n += 1
n = 0
while n <= 29:
	n += 3
	print(n)

1~20求和累加

sum1 = 0
i = 1
while i <= 20:
	sum1+=i
	i+=1

print(sum1)

sum1永远都是拿上一步的计算结果,参与到本次计算中

打印三角形

ceng = 1
while ceng<=5:
	print('*'*ceng)
	ceng += 1

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

打印乘法表

i = 0
while i < 9:
	i += 1
	j = 0
	while j < 9:
		j += 1
		if i >= j:
			print(j,'*',i,'=',i*j,end=' ',sep='')
	print()
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
posted @ 2020-02-09 13:55  多走多看  阅读(290)  评论(0)    收藏  举报