Python条件控制

#"""学习和测试条件控制(单独使用和嵌套使用)"""
#在prompt和jupyter中多行注释都无效
#测试单独使用
name = input("place input you name : ")#默认传入的是string
mid = input("place input you age : ")
if mid.isdigit(): #判断是否为数字
age = int(mid) #转换为int ,否则直接使用if age <= 18:报错,报错信息:TypeError: '<=' not supported between instances of 'str' and 'int'
else:
while not mid.isdigit(): #在条件满足时一直执行while内部的代码块,直到条件不满足时跳出循环,执行下面的代码
mid = input("place input you age : ")
age = int(mid)
if(name == "zhangsan"): #带括号不报错,不建议使用
  print("Hello " + name)
if name == "zhangsan":
  print("Hello " + name)
elif name == "":
  name = input("place again input you name : ") #单独使用弊端:不能二次验证
print("Hello " + name)
else:
  print("who you are !")
#测试嵌套
if name == "zhangsan":
if age <= 18:
  print(name + "is a children !")
elif age >= 60:
  print("time is flying !")
  age = age - 60;
print("Hello " + name + " you age is back to " + str(age)) #上面已经转为int,不能进行直接字符串拼接,报错信息:TypeError: must be str, not int
elif name == "":
  name = input("place again input you name : ")
while name == "": #在条件满足时一直执行while内部的代码块,直到条件不满足时跳出循环,执行下面的代码
  name = input("you input is null,place again input you name : ")
print("Hello " + name)
else:
  print("who you are !")

结果:

 

posted @ 2018-06-05 14:34  云山之巅  阅读(328)  评论(0编辑  收藏  举报