标签:cte style ted log 代码 dig 不同的 put 设置
1. if语句
1.1 功能
if语句主要用来根据周围环境条件(即expession)的变化做出不同的反应(即执行代码)
1.2 语法
1.2.1 单分支结构
if单分支单条件:
if expression: expr_true_suite 注释:expession为真执行代码expr_true_suite
示例:
name = ‘test‘ input_name = input("input_name:") if name == input_name : print("right!")
if单分支多条件:
if expression1 and expression2 : #条件两边可用and 或or expr_true_suite
注释:expession为真执行代码expr_true_suite
示例:
name = ‘test‘ age = 22 input_name = input("input_name:") input_age = int(input("input_age:") ) if name == input_name and age == input_age : print("name and is right!")
if+else:
if expression: expr_true_suite else: expr_false_suite
示例:
name = ‘test‘ input_name = input("input_name:") if name == input_name: print("name is right!") else: print("name is error")
1.2.2 多分支结构
if多分支结构:
if expession1: expr1_true_suite elif expression2: expr2_true_suite elif expession3: expr3_true_suite else: none_of_the_above_suite
示例:
name = ‘test‘ age = 22 input_name = input("input_name:") input_age = int(input("input_age:") ) if name !=input_name and age !=input_age : print("name and age all error!") elif name == input_name and age !=input_age : print("name right and age error") elif name != input_name and age ==input_age : print("age right and name error") else: print("all right")
1. while循环
在未到达设置的条件前(或没有限制条件)时,会一直执行。
猜年龄优化:
输入三次不对则退出
count = 0 self_age = 56 while count <3: guess_age = int(input("guess_age:")) # if guess_age.isdigit(): # guess_age == int(guess_age) # else: # continue if guess_age == self_age: print("you are right!!!!") break elif guess_age < self_age: print("try bigger!!!") else: print("try small!!!!") count +=1 else: print("try too many is error ! byebye....")
标签:cte style ted log 代码 dig 不同的 put 设置
原文地址:http://www.cnblogs.com/wuchunwei/p/5987529.html