码迷,mamicode.com
首页 > 编程语言 > 详细

python流程控制

时间:2017-09-04 09:59:44      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:div   ssi   tin   ctr   exe   end   判断   use   int   

>>> True or False and False
True
首先判断前两个所以得到True之后就不用判断了
#########################
>>> (True or False) and False
False
###########
流程控制
if...else
单分支
if 条件:    :是语法格式
    满足条件后执行代码   缩进4个空格
	这是判断成立后执行的代码
AgeOfOldboy = 56
if AgeOfOldboy > 50 :
    print("Too old,time to retire..")
加注释快捷键  ctrl+/
双分支
age_of_oldboy=50
if age_of_oldboy > 100:
    print(‘too old,time to end‘)
else:
    print(‘impossible‘)
	
age_of_oldboy=50
age_input=int(input(‘input age:‘))
if age_input > age_of_oldboy:
    print(‘too big‘)
elif age_input < age_of_oldboy:
    print(‘too small‘)
else:
    print(‘you get it‘)

#########练习############
成绩ABCDE五个等级与分数对应如下
A 90-100
B 80-89
C 60-79
D 40-59
E 0-39
要求用户输入0-100的数字,能正确的打印成绩
scroce = int(input("输入分数:"))
if scroce > 100:
    print("我擦,最高分才100")
elif scroce >= 90:
    print("A")
elif scroce >= 80:
    print("B")
elif scroce >= 70:
    print("C")
elif scroce >= 60:
    print("D")
else:
    print("E")
###################################
循环
从1打印到3
count=1
while count <= 3:
    print(count)
    count+=1
###################################
在等于10的时候跳出循环
count=1
while count <= 100:
    if count == 10:
        break #跳出本层循环
    print(count)
    count+=1
###################################
count=0
while count <5:
    if count == 3:
        count+=1
        continue
    print(count)
    count+=1
1、count=0 满足<5
   print 0
   0+1=1
2、count=1 满足<5
   print=1 
   1+1=2
3、count=2 满足<5
   print=2
   2+1=3
4、count=3 满足<5
   满足if count == 3:
   因为continue是跳出本次循环所以
   count+=1可以是循环不在这里卡住
   3+1=4
5、count=4 满足<5
   print=4
   因循环必须小于5所以停止循环
##############
打印0-10的偶数
count=0
while count <= 10:
    if count % 2 == 0:
        print(count)
    count+=1
#################
count=0
while count <= 10:
    if count % 2 == 0:
        count+=1
        continue
    print(count)
    count+=1
打印奇数
####################
打印小于3并且大于96的数(0-100)
count=1
while count <= 100:
    if count >=3 and count <=96:
        count+=1
        continue
    print(count)
    count+=1
###########################
猜年龄只能猜3次
age_of_oldboy=50
count=1
while count <=3:
    age_inp=int(input(‘input age:‘))
    if  age_inp > age_of_oldboy:
        print(‘too big‘)
        count+=1
    elif age_inp < age_of_oldboy:
        print(‘too small‘)
        count+=1
    else:
        print(‘you get ot‘)
        break
#################################
无限循环
while True:
    cmd=input(‘>>: ‘)   #int只能把123这种字符串转换为形式
    if cmd == ‘q‘:
        break
#################################
输入账户密码3次机会
count=0
while True:
    if count > 2:
        print(‘too many tries‘)
        break
    user=input(‘user: ‘)
    password=input(‘password ‘)
    if user == ‘egon‘ and password == ‘123‘:
        print(‘login successful‘)
        break
    else:
        print(‘login err‘)
        count+=1
count=0
while count <3:
    if count > 2:
        print(‘too many tries‘)
        break
    user=input(‘user: ‘)
    password=input(‘password ‘)
    if user == ‘egon‘ and password == ‘123‘:
        print(‘login successful‘)
        break
    else:
        print(‘login err‘)
        count+=1
#############################
count=0
tag=True
while tag:
    if count > 2:
        print(‘too many tries‘)
        break
    user=input(‘user: ‘)
    password=input(‘password ‘)
    if user == ‘egon‘ and password == ‘123‘:
        print(‘login successful‘)
        while tag:
            cmd=input(‘>>: ‘)
            if cmd == ‘q‘:
                tag=False
                continue
            print(‘exec %s‘ %cmd)
    else:
        print(‘login err‘)
        count+=1
#############
while else  连用
count=1
while count <=5 :
    print(count)
    count+=1
else: #while 没有被break打断的时候才执行else的子代码
    print(‘=========>‘)
	
count=1
while count <=5 :
    if count ==2 :
        break
    print(count)
    count+=1
else: #while 没有被break打断的时候才执行else的子代码
    print(‘=========>‘)

  

python流程控制

标签:div   ssi   tin   ctr   exe   end   判断   use   int   

原文地址:http://www.cnblogs.com/zhaohongyu6688/p/7471978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!