文章目录

  • Educoder—第2章 Python 分支结构
    • 第1关:Python单路分支之求平抛小球与抛出点之间的距离
    • 第2关:Python单路分支之正方形判断
    • 第3关:Python双路分支之温度转换
    • 第4关:Python双路分支之闰年的判断
    • 第5关:Python多路分支之天数判断
    • 第6关:Python多路分支之一元二次方程求解

Educoder—第2章 Python 分支结构

本章目标是通过学习具备编写 Python 分支结构程序的能力,涉及的 Python 编程基本概念包括:布尔数据类型( boolean ),关系运算符、逻辑运算符及表达式,分支语句 if、if-lese、if-elif-else 等。

第1关:Python单路分支之求平抛小球与抛出点之间的距离

任务:一小球以 5米/秒 的水平速度平抛,重力加速度取9.8米每秒的平方,在忽略空气阻力的情况下,求经过时间 t 秒后(t 是获取的输入值),小球所在位置与抛出点之间的距离 (假设小球距地面足够高,t应大于0)。

如果t>0,输出格式为:"经过t秒后,小球与原点的距离为d米"

如果t<0,输出格式为:"t<0,不合法"

from math import*
G = 9.8     # 声明浮点型变量 G,用于表示重力加速度
v0 = 5      # 声明整型变量 v0, 用于表示水平初速度t = int(input())
s = v0 * t
h = 0.5 * G * pow(t,2)
d = sqrt((pow(s,2) + pow(h,2)))
if t > 0:print("经过%.6f秒后,小球与原点的距离为%.6f米"%(t,d))
else :print("t<0,不合法")执行结果;
D:\网络安全\Python\py_code>python educ.py
please input time:1
经过1.000000秒后,小球与原点的距离为7.000714米D:\网络安全\Python\py_code>python educ.py
please input time:5
经过5.000000秒后,小球与原点的距离为125.024998米

第2关:Python单路分支之正方形判断

任务:假设现在有一个方形,它的长度和宽度未知,只知道长和宽的变量名,请编写代码判断该方形是否为正方形(长和宽都应大于 0)。

输出格式:

如果长度小于等于0输出"长度不合法",

如果宽度小于等于0,则输出"宽度不合法",

如果长度等于宽度,则输出"该方形为正方形",

如果长度不等于宽度,则输出"该方形为长方形"。

leng = int(input("please input length:"))
wid = int(input("please input width:"))
if leng<=0:print("长度不合法")
elif wid<=0:print("宽度不合法")
elif leng==wid:print("该方形为正方形")
elif leng!=wid:print("该方形为长方形")执行结果:
D:\网络安全\Python\py_code>python educ.py
please input length:1
please input width:2
该方形为长方形D:\网络安全\Python\py_code>python educ.py
please input length:1
please input width:1
该方形为正方形D:\网络安全\Python\py_code>python educ.py
please input length:-1
please input width:-2
长度不合法

第3关:Python双路分支之温度转换

任务:根据输入的选项,完成从摄氏度到华氏度或从华氏度到摄氏度的转换。

输入数据包括温度的单位、待转换的温度值,温度值为浮点型。摄氏度的单位可能为摄氏度,也可能为 C,华氏度的单位可能为华氏度,也可能为 F

输出格式为:"c摄氏度转换为f华氏度"

while True:unit = str(input("please input the unit:"))value = float(input("please input the value:"))if unit == "C" or  unit == "摄氏度":f = value*1.8+32print("{:.6f}摄氏度转换为{:.6f}华氏度".format(value,f))elif unit == "F" or  unit == "华氏度":c = (value-32)/1.8print("{:.6f}华氏度转换为{:.6f}摄氏度".format(value,c))执行结果;
D:\网络安全\Python\py_code>python educ.py
please input the unit:摄氏度
please input the value:10
10.000000摄氏度转换为50.000000华氏度
please input the unit:C
please input the value:15
15.000000摄氏度转换为59.000000华氏度
please input the unit:F
please input the value:39.8
39.800000华氏度转换为4.333333摄氏度
please input the unit:华氏度
please input the value:32
32.000000华氏度转换为0.000000摄氏度
please input the unit:C
please input the value:-1
-1.000000摄氏度转换为30.200000华氏度

第4关:Python双路分支之闰年的判断

任务:假设现在我们已知年份为 year,请编写代码判断这一年是否为闰年。

输出格式:"year年是闰年"或者"year年是平年"

分析:判断一个年份是否为闰年主要取决于两点,年份能被 400 整除,或者能被 4 整除但不能被 100 整除的就是闰年。假设现在我们已知年份为 year,请编写代码判断这一年是否为闰年。

while True:year = int(input("please input the year:"))if year%400 == 0 or (year%4==0 and year%100!=0):print("{}年是闰年".format(year))else:print("{}年是平年".format(year))执行结果:
D:\网络安全\Python\py_code>python educ.py
please input the year:2020
2020年是闰年
please input the year:1984
1984年是闰年
please input the year:2017
2017年是平年
please input the year:1500
1500年是平年

第5关:Python多路分支之天数判断

任务:根据输入的年份和月份判断该月的天数。

一年中,1、3、5、7、8、10、12月有31天,4、6、9、11月有30天,

闰年的2月有29天,平年的2月有28天。

输出格式:"year年month月有30天"

做法1:根据闰平年来做区分
year = int(input("please input the year:"))
month = int(input("please input the year:"))
if year%400 == 0 or (year%4==0 and year%100!=0):if month == 2:print("{}年{}月有29天".format(year,month))elif month == 4 or month == 6 or month == 9 or month == 11print("{}年{}月有30天".format(year,month))else:print("{}年{}月有31天".format(year,month))
else:if month == 2:print("{}年{}月有28天".format(year,month))elif month == 4 or month == 6 or month == 9 or month == 11print("{}年{}月有30天".format(year,month))else:print("{}年{}月有31天".format(year,month))做法2:根据2月这个特殊月份来做区分,建议用做法2这种,即找到大的区分
while True:year = int(input("please input the year:"))month = int(input("please input the year:"))if month == 4 or month == 6 or month == 9 or month == 11:print("{}年{}月有30天".format(year,month))elif month == 2:if year%400 == 0 or (year%4==0 and year%100!=0):print("{}年{}月有29天".format(year,month))else:print("{}年{}月有28天".format(year,month))else:print("{}年{}月有31天".format(year,month))执行结果:
D:\网络安全\Python\py_code>python educ.py
please input the year:2020
please input the year:2
2020年2月有29天
please input the year:2019
please input the year:7
2019年7月有31天
please input the year:1965
please input the year:2
1965年2月有28天

第6关:Python多路分支之一元二次方程求解

任务:求解一元二次方程 ax2+bx+c=0 的根,系数 a、b、c 的值从输入获取。(本关a,b,c都是整型)

from math import *while  True:a = int(input("please input a:"))b = int(input("please input b:"))c = int(input("please input c:"))if a==0:x = -(c/b)print(x)elif  (pow(b,2)-4*a*c) > 0:y1 = (-b+sqrt(pow(b,2)-4*a*c))/(2*a)y2 = (-b-sqrt(pow(b,2)-4*a*c))/(2*a)print("x1为{:.6f},x2为{:.6f}".format(y1,y2))elif (pow(b,2)-4*a*c)==0:z = -b/(2*a)print(z)else:print("无解")执行结果:
D:\网络安全\Python\py_code>python educ.py
please input a:0
please input b:1
please input c:3
-3.0
please input a:2
please input b:4
please input c:2
-1.0
please input a:3
please input b:3
please input c:1
无解
please input a:4
please input b:5
please input c:1
x1为-0.250000,x2为-1.000000

please input b:1
please input c:3
-3.0
please input a:2
please input b:4
please input c:2
-1.0
please input a:3
please input b:3
please input c:1
无解
please input a:4
please input b:5
please input c:1
x1为-0.250000,x2为-1.000000


第2章 Python 分支结构相关推荐

  1. 第3章 Python 循环结构

    文章目录 Educoder-第3章 Python 循环结构 第1关:Python循环结构之while循环生成温度转换表 第2关:Python循环结构之while循环实现凯撒密码加密 第3关:Pytho ...

  2. python中常见的流程结构-Python分支结构(switch)操作简介

    Python当中并无switch语句,本文研究的主要是通过字典实现switch语句的功能,具体如下. switch语句用于编写多分支结构的程序,类似与if-.elif-.else语句. switch语 ...

  3. Python分支结构详解(一)——分支结构基础

    今天继续给大家介绍Python相关知识,本文主要内容是Python分支结构基础. 一.单分支结构 Python语言单分支结构中使用if语句,格式如下: if [条件]:[语句块] 在上述代码中,如果条 ...

  4. python分支结构的关键字_学习python分支结构

    学习python分支结构 来源:中文源码网    浏览: 次    日期:2019年11月5日 [下载文档:  学习python分支结构.txt ] (友情提示:右键点上行txt文档名->目标另 ...

  5. Python分支结构

    Python分支结构 Python的分支结构就是C语言中的判断语句类型,Python简洁明了的代码格式能让你更清醒的认识编写Python代码的乐趣,特别是在pycharm中写代码,代码自动补全,自动缩 ...

  6. python中分支结构有几种各有什么特点_「武鹏有课」Python分支结构的种类

    下面我们学习Python的分支语句结构: 第一种分支"if",if是如果的意思,是一个关键字,在if的后面要跟一个条件表达式,是什么样的条件表达式呢?这个表达式是由"比较 ...

  7. Python分支结构你真的搞定了吗?

    分支结构 分支结构能够让计算机像人一样进行思考,应对不同的场景做出不同的回应. Python中不支持switch语法,目前仅支持if/else形式,但是在Python3.10的测试版本中,貌似支持了s ...

  8. python分支结构基础实训_零基础Python教程-分支结构

    文章目录 下面就让我们先一起学习单分支结构: 单分支结构 双分支结构 多分支结构 在日常生活中,我们会遇到很多选择类的问题.比如:午饭吃螺蛳粉还是生煎包?下班回家坐地铁还是公交车?这也就是我们编程语言 ...

  9. python分支结构说课_Python_3.8平台上的分支结构(模块.类.函数)_11

    计算机 python语言_3.8平台上的分支结构(模块.类.函数)11 上节说了,python程序有注释.缩进和程序主题.其应用软件由模块--文件*.py分割保存.模块中有变量.函数.类(数据与函数) ...

最新文章

  1. mysql 集群切换_完美起航-MySQLMHA高可用集群部署及故障切换(图文详解)
  2. 南京大学人工智能本科专业教育培养体系
  3. 【干货】从零开始做运营(超详细脑图)
  4. 当遭遇“用户增长”停滞,你应该怎么办?
  5. lis25ba_LIS25BA - MEMS数字输出运动传感器:低噪声,高带宽,3轴加速度计,带TDM接口 - STMicroelectronics...
  6. OpenCV中VideoCapture判断isOpened()时总是返回false
  7. Linux htop工具使用详解
  8. 要开始算法了 什么顺序呢?
  9. Flask学习笔记之:jinja2变量过滤器以及自定义过滤器
  10. 网络正常但Chrome不能上网的解决方法
  11. rwd是什么意思_为什么RWD看起来像RWD
  12. jad158g class文件转java文件
  13. 【行业首发】蛙色VR全景作品支持对接高德地图!
  14. matplotlib画图(一)——线条图
  15. 【狂神】HTML笔记
  16. linux 拍照软件有哪些,六款基于Linux的开源照片管理软件推荐
  17. OpenSSL/GmSSL 动态引擎
  18. uniapp展示富文本
  19. 学习笔记:计算excel中的平均值并去除0值
  20. 完美世界2018年净利润17亿元 同比增长13.16%

热门文章

  1. 读书笔记——《802.11无线网络权威指南》
  2. 那些花儿(夏洛特烦恼版)
  3. 各种本地存储对比 cookie,localStorage,sessionStorage,indexDB以及他们和vuex的区别
  4. linux内核协议栈接收数据流程(一)
  5. 第一行代码读书笔记(Chapter2 探究新语言,快速入门Kotlin编程)
  6. 展望:可见光通信技术标准体系建设
  7. 记录Win10正确安装CUDA和cuDNN的过程(记录一些坑)
  8. selenium+python登录新浪微博
  9. 变电站可视化搭建推陈出新?无人值守却更胜一筹
  10. [Luogu P4168] [BZOJ 2724] [Violet]蒲公英