python之路-03-Python语法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之路-03-Python语法相关的知识,希望对你有一定的参考价值。

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语法的主要内容,如果未能解决你的问题,请参考以下文章

pytho 的基本语法

Pytho基础要点:7种复合语句在编写时要遵循的语法风格

Python之路

Python之路——内置模块总结

Python 03-Python3基础语法

Python之路--目录