第2章 Python 分支结构

文章目录

  • 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+32
	    print("{:.6f}摄氏度转换为{:.6f}华氏度".format(value,f))
	elif unit == "F" or  unit == "华氏度":
	    c = (value-32)/1.8
	    print("{:.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 == 11
        print("{}年{}月有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 == 11
        print("{}年{}月有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
20202月有29天
please input the year:2019
please input the year:7
20197月有31天
please input the year:1965
please input the year:2
19652月有28

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

任务:求解一元二次方程 ax2+bx+c=0 的根,系数 a、b、c 的值从输入获取。(本关a,b,c都是整型)
第2章 Python 分支结构_第1张图片

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


你可能感兴趣的:(#,Edu-Python程序设计,python)