python之if循环


if 条件:
  if语句块
else:
  语句块

money = int(input("请输入你兜里的钱:"))
if money > 500:
    print("吃肉")
    print("喝酒")
    print("聊天")
else:
    print("吃面包")
    print("老干妈配馒头")
    print("盖浇饭")

 

if 条件:
  if语句块
elif 条件:
  语句块
....
else:
  else语句块
#if...elif...else 有一个成立,就不执行其他.

例:

score = int(input("请输入你的分数:"))
if score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")
elif score >= 70:
    print("中等")
elif score >= 60:
    print("及格")
else:
    print("不及格")

  

if嵌套
嵌套的层数不要太多,一般不超过3-5层

例:

print("咣咣咣")
gender = input("请输入你的性别:")
if gender == "":
    print("隔壁有人在等你")
else: # 不是男
    age = int(input("请问你的年龄是:"))
    if age >= 25:
        print("去隔壁,隔壁有人在等你")
    else:
        length = int(input("请问你的身高:"))
        if length >= 200:
            print("快去隔壁")
        else:
            print("进来吧")

 

原文地址:https://www.cnblogs.com/q455674496/p/10022758.html