python中的两种语句:
1、if条件控制语句
格式:
if:
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a < b:
    print("a比b小")
if-else:
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a < b:
    print("a比b小")
else:
    print("a比b大")
 
a = int(input("请输入一个三位数:"))
#153  水仙花数含义:153=1^3 + 5^3 + 3^3
q = a % 10
w = a // 10 % 10
e = a //100
if a == q**3 + pow(w,3) + e**3:
 print("是水仙花数")
else:
 print("不是水仙花数")
 
if-elif-else:
age = int(input("请输入您的年龄:"))
if age <= 0 :
 print("娘胎里")
elif age < 3 :
 print("儿童")
elif age < 6 :
 print("少年")
elif age < 18 :
 print("成年")
elif age < 30 :
 print("青年")
elif age < 50 :
 print("中年")
elif age < 100 :
 print("老年")
elif age < 150 :
 print("老寿星")
else:
 print("老妖怪")
 
2、while、for循环语句
while语句:
while 表达式:
 语句1
 
逻辑:当程序执行到while语句时,首先计算表达式的式的值,当表达式的值为假,结束整个语句,如果表达式的值为真,则执行语句,执行完语句再次计算表达式的值
 
while 表达式:
 语句1
else:
 语句2
逻辑:当表达式执行为false时,执行else 
def BMI():
    if bmi < 18.5:
        # 下面 2 行同属于 if 分支语句中包含的代码,因此属于同一作用域
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("体重过轻")
    if bmi >= 18.5 and bmi < 24.9:
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("正常范围,注意保持")
    if bmi >= 24.9 and bmi < 29.9:
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("体重过重")
    if bmi >= 29.9:
        print("BMI指数为:"+str(bmi)) #输出BMI指数
        print("肥胖")
age = int(input("请输入您当前的年龄"))
while age >= 18 and age <= 25:
    h = float(input("请输入您当前身高"))
    w = float(input("请输入您当前体重"))
    bmi = w / h ** 2
    BMI()
    age += 1
else:
    print("我们只看18-25岁的哦")
 
 
for语句:
for 变量名 in  集合:
 语句
逻辑:按顺序取集合中的每个元素,赋值给变量,再去执行语句,如此循环往复,直到取完集合中的元素截止
 
def BMI():
    if bmi < 18.5:
        # 下面 2 行同属于 if 分支语句中包含的代码,因此属于同一作用域
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("体重过轻")
    if bmi >= 18.5 and bmi < 24.9:
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("正常范围,注意保持")
    if bmi >= 24.9 and bmi < 29.9:
        print("BMI指数为:" + str(bmi))  # 输出BMI指数
        print("体重过重")
    if bmi >= 29.9:
        print("BMI指数为:"+str(bmi)) #输出BMI指数
        print("肥胖")
a = int(input("请输入您的年龄"))
for age in range(18,25):
    #print(age)
    if a == age:
        h = float(input("请输入身高"))
        w = float(input("请输入体重"))
        bmi = w / h ** 2
        BMI()
        a += 1
    else:
        print("我们只看18-25岁的哦")
 
 
 
 
 
 
 

最新文章

  1. CSS魔法堂:重拾Border之——更广阔的遐想
  2. Apache Marmotta 3.1.0-incubating 发布
  3. 遍历所有的选中的radio的个数和值
  4. Java MD5加密工具类
  5. [LeetCode] Longest Palindromic Substring(manacher algorithm)
  6. PAT (Basic Level) Practise:1008. 数组元素循环右移问题
  7. cxf2.7.10与Spring3.0.5集成时报错如下
  8. 第3.3.4节 创建高级图形之RenderScript(二)
  9. 维吉尼亚密码java代码实现根据密钥长度计算IC值过程
  10. ubuntu上安装apache2+mysql+php5-fpm(PHP5 - FastCGI Process Manager)
  11. 解决:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression &#39;requestMap.maintenancename != null and requestMap.maintenance
  12. cocos 动画系统
  13. centos7安装与配置nginx1.11,开机启动
  14. Pytorch报错记录
  15. 根据CAS协议写的简单的SSO框架
  16. ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(4)
  17. 分享微信开发Html5轻游戏中的几个坑
  18. Nuget包CommonServiceLocator从1.0.3升级到2.0.4时MvvmLight的ViewModelLocator初始化SimpleIoc.Default格式不匹配问题
  19. linux中awk工具的使用(转载)
  20. Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小

热门文章

  1. windows server 2008 R2 怎么集成USB3.0驱动
  2. OpenGL_构建GLFW与第一个程序
  3. NOIp2013D2T3 华容道【搜索&amp;图论-最短路】
  4. 【DSP开发】C6000非多核非KeyStone系列DSP中断系统
  5. 协程,纤程(Fiber),或者绿色线程(GreenThread)
  6. Interceptors - 拦截器
  7. Elasticsearch-数组和多字段
  8. redis缓存雪崩
  9. 坦克大战--Java类型 ---- (3)实现socket通信
  10. linux centos 7.3 编译安装mysql5.7