栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python

流程控制
顺序流程:每一行代码都会执行,并且只会执行一次
···
if --  else 结构语句:
1、if 布尔表达式:
代码对齐,tab与空格不能够同时出现

2、if 布尔表达式:
代码
else:

money = input ('please input your money:')
if money > 100:
print 'you are vip!'
print 'you are gold VIP'
else :
print('you are not vip!')

3、if--else 嵌套:
if 布尔表达式:
代码
else:
if  布尔表达式:
代码
else:
代码
....

money = input ('please input your money:')
if money > 100:
print 'you are vip!'
if money >= 200:
print 'you are golden VIP'
if 200>money>100:
print'you are silver vip'
else :
if money<0:
print'error!'
else:
print('you are not vip!')

4、if 布尔表达式:
代码
elif 布尔表达式:
代码

money = input ('please input your money:')
if money > 100:
print 'you are vip!'
if money >= 200:
print 'you are golden VIP'
if 200>money>100:
print'you are silver vip'
elif money<0:
print'error!'
else:
print('you are not vip!')

EG:
第一种:
#coding:UTF-8
score = int(input('please input a score:'))

if 100>=score>=90:
print('很好')
if 90>=score>=70:
print('well done!')
if 70>=score>=60:
print('pass!')
if 60>score:
print('failed!')
if score>100 or score<0:
print ('error!')

第二种:

-- coding: cp936 --

score = int(input('please input a score:'))

if 100>=score>=90:
print('很好')
else:
if 90>=score>=70:
print('良好')
else:
if 70>=score>=60:
print('及格')
else:
if 60>score>=0:
print('不及格')
else:
print ('error!')
第三种:    
score = int(input('your number:'))
if 100>=score>=90:
print('excellent')
elif 90>=score>=70:
print('good')
elif 70>=score>=60:
print('pass')
elif 60>score>=0:
print('flunk')
else:
print ('error!')

循环流程
··while 循环

重复运行某些代码
1、语法:
while 布尔表达式:
代码:(循环体)
注意:要避免死循环(注意循环跳出条件)
服务器就是死循环724365
num = 0
while num<10:
print 'hello world!'
num+=1

·····for循环
1、语法:
for 目标 in 表达式:
循环体

                            目标:变量                            表达式:字符串、列表、元祖----可迭代对象

a = 'baizhi'
for i in a:
print i

teacher = ['fei','xige','kuai']
for i in teacher:
print i

number = [123,466,969]
for i in number:
print i

·range()
语法:
range([start],stop[,step=1])
1、这个函数有三个参数,其中方括号表示可选择参数()
2、第一个参数默认就是0,第二个参数停止,第三默认1
3、作用生成一个值,从start开始,到stop结束的数字序列
取值范围从[start,stop)左闭右开

a = range(0,3,1)

>> a
[0, 1, 2]
>>
>> a=range(0,10,2)
>> a
[0, 2, 4, 6, 8]
>> a = range(0,10,3)
>> a
[0, 3, 6, 9]
>> range(3)
[0, 1, 2]

for i in range(5):
print i

01
2
3
4

>> for i in range(2,5):
print i

2
3
4

>> for i in range(1,5,2):
print i

1
3

··break
跳出当前循环,后面所有循环都不执行

while True:
num = input('please input your number:')
if num ==1:
break
print('error,try again!')

please input your number:3
error,try again!
please input your number:1

·continue
跳过本次循环,不影响下次循环

for i in range(10):
if i%2!=0:
continue
print i

02
4
6
8

>> for i in range(10):
if i%2!=0:
break
print i

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号