python之路-03-Python语法

本文详细介绍了Python中的流程控制语句,包括if...else、if...elif...else、while...else及for...else等条件和循环控制结构,并通过具体示例进行说明。

3.1 if流程判断

3.1.1 if else流程判断

#!Author:lanhan
_username = 'lanhan'
_password = '123'
username = input("username:")
password = input("password:")
if username == _username and password == _password:
    print("welcome user {name} login...".format(name=username))
else:
    print("Invalid username or password!")

 

3.1 .2if...elif...else流程判断

#!Author:lanhan
age_of_oldboy = 56
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
    print("yes,you got it. ")
elif guess_age > age_of_oldboy:
    print("think smaller...")
else:
    print("think bigger! :")

3.2 while流程判断

3.2.1while True

实例1

#!Author:lanhan
count = 0
while True:
    print("count:",count)
    count = count +1
    if count == 3:
        break

 

实例2

#!Author:lanhan
count = 0
age_of_oldboy = 56
while True:
    if count == 3:
        break
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
         print("think bigger! :")
    count +=1

3.2.2while...else

 

示例1

#!Author:lanhan
count = 0
age_of_oldboy = 56
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it. ")
        break        #####不往下走了,直接跳出整个循环
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
         print("think bigger! :")
    count +=1
else:
    print("you have tried too many times..fuck off")

3.3 for流程判断

3.3.1 for i in 变量

示例1

#!Author:lanhan
for i in range(0,10,2):
    if i < 3:
        print("loop",i)
    else :
        continue         ####跳出本次循环,继续下次循环
    print("hehe.....")

 

3.3.2for...else

示例1

#!Author:lanhan
count = 0
age_of_oldboy = 56
for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes,you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
         print("think bigger! :")
else:
    print("you have tried too many times..fuck off")

转载于:https://www.cnblogs.com/decorator/p/7898767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值