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

python之路-03-Python语法

时间:2017-11-26 13:57:06      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:mat   if...else   ##   input   invalid   pre   pytho   lan   语法   

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")

python之路-03-Python语法

标签:mat   if...else   ##   input   invalid   pre   pytho   lan   语法   

原文地址:http://www.cnblogs.com/decorator/p/7898767.html

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