Python::判断语句流程控制

来源:互联网 发布:刚毕业的程序员工资 编辑:程序博客网 时间:2024/06/01 19:06

一个简单的判断密码是否一致的栗子:

name = "sam"

password = "hello"

_name = input("name:")

_password = input("password:")

if _name == name and _password == password :

print("{name} login successfully".format(name = name))

else: 

 print("Wrong password or name!")

Python有强制缩进,因此不需要{},或者fi之类标记,并且结构清晰。


if...elif..以及判断嵌套

age_of_taylor = 29
age = int(input("guess the age:"))if age == age_of_taylor:    print("right")    if age ==29:        print("very right")elif age>age_of_taylor:    print("think bigger")else:    print("think smaller")
与C/C++不同,Python使用elif
加上while循环后的猜年龄游戏(只能猜3次)
age_of_taylor = 29count = 0while count < 3:    age = int(input("guess the age:"))    if age == age_of_taylor:        print("right")        break    elif age>age_of_taylor:        print("think bigger")    else:        print("think smaller")    count+=1else:    print("too many times")       //独特的语法


原创粉丝点击