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

Python之流程控制语句

时间:2018-09-18 23:56:37      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:金字塔   bsp   游戏   输出   使用   class   int   name   range   

#一.流程控制之if ..else语句
if 条件1:
    pass
elif 条件2:
    pass
elif 条件3:
    pass
else:
    pass


#1.简单的if打印
age_of_girl=31
if age_of_girl > 30:
    print(阿姨好)

#2.if ..else 单分支
age_of_girl=31
if age_of_girl > 30:
    print(阿姨好)
else:
    print(小姐好)

#3.多个条件判断
age_of_girl=18
height=171
weight=99
is_pretty=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 weight < 100 and is_pretty == True:
    print(表白)
else:
    print(阿姨好)


#4.多分支if..else 
score=input(>>: )
score=int(score)
if score >= 90:
    print(优秀)
elif score >=80:
    print(良好)
elif
    print(普通)
else:
    print(很差)


#二流程控制之while循环
#1.打印0-10
count=0
while count <= 10:
    print(loop,count)
    count+=1

#2.打印0-10之间的偶数
count=0
while count <= 10:
     if count%2 == 0:
             print(loop,count)
         count+=1


#3.打印0-10之间的奇数
count=0
while count <= 10:
    if count %2 == 1:
            print(loop,count)
        count+=1

#break用于退出本层循环
while True:
    print "123"
    break
    print "456"

#continue用于退出本次循环,继续下一次循环
while True:
    print "123"
    continue
    print "456"

#4.使用while循环输出1 2 3 4  5 6 8 9 10
count=1
while count <= 10:
    if count == 7:
        count+=1
        continue
    print(count)
    count+=1

#5.求100内的所有数的和
count=0
while count <= 100:
    res+=count
    count+=1
print(res)

#6.输出1-100内的所有的奇数
count=0
while count <= 100:
    if count%2 != 0:
        print(count)
    count+=1
#7.输出1-100内的所有的偶数
count=0
while count <= 100:
    if count%2 = 0:
        print(count)
    count+=1
#8.求1-2+3-4+5....99的所有数的和
res=0
count=1
while count <= 5:
    if count%2 == 0
        res-=count
    else:
        res+=count
    count+=1
print(res)
#9 用户登陆(三次机会重试)
count=0
while count < 3:
    name=input(请输入用户名:)
    password=input(请输入密码:)
    if name == egon and password == 123:
        print(login succcess)
    else:
        print(用户名或者密码错误)
        count+=1

#10.猜年龄游戏
#允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
#如果猜对了,就直接退出 
age=73
count=0
while True:
    if count == 3:
        choice=input(继续Y/N)
        if choice == Y or choice == y:
                count=0
        elsebreak
guess=int(input(>>>))
if guess == age:
    print(you  got it)
    break
count+=1


#三.流程控制之for循环
打印金字塔
max_level=5
for current_level in range(1,max_level+1):
    for i in range(max_level-current_level):
        print( ,end=‘‘) #在一行中连续打印多个空格
    for j in range(2*current_level-1):
        print(*,end=‘‘) #在一行中连续打印多个空格
    print()

 

Python之流程控制语句

标签:金字塔   bsp   游戏   输出   使用   class   int   name   range   

原文地址:https://www.cnblogs.com/zhangcaiwang1/p/9672127.html

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