热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python数据结构03分支结构

1.#单分支结构#1#输入两个整数存放于变量a和b中,并使得a中存放的数据小于b中存放的数据aint(input(Pleaseinput1stinteger:)

1.

# 单分支结构#1
# 输入两个整数存放于变量a和b中,并使得a中存放的数据小于b中存放的数据
a = int(input('Please input 1st integer: '))
b = int(input('Please input 2nd integer: '))
print('Before processing: ')
print('a=%d,b=%d'%(a,b))
if a>b:t=aa=bb=t
print('After processing: ')
print('a=%d,b=%d'%(a,b))

C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/单分支结构——001.py
Please input 1st integer: 3
Please input 2nd integer: 2
Before processing:
a=3,b=2
After processing:
a=2,b=3

Process finished with exit code 0

2. 双分支结构

# 双分支结构#1
# 比较两个数的大小,输入两个整数,输出较大的数
a = float(input('Please input 1st number: '))
b = float(input('Please input 2nd number: '))
if a>b:print(a)
else:print(b)

C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/双分支结构#1.py
Please input 1st integer: 3.6
Please input 2nd integer: 7.3
7.3

# 双分支结构#2
# 输入三条边的值,看是否组成三角形
a = float(input('Please input 1st number: '))
b = float(input('Please input 2nd number: '))
c = float(input('Please input 3rd number: '))
if a+b>c and b+c>a and a+c>b:print("可以组成三角形")
else:print("不可组成三角形")

C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/双分支结构#2.py
Please input 1st number: 2
Please input 2nd number: 3
Please input 3rd number: 4
可以组成三角形

# 双分支结构#3
# 划船问题,一个教师带着x个学生去划船,每条船最多装4人,问需要几条船
a = int(input('Please input 学生数目: '))
total = a+1 #总人数将老师也算在内
if total % 4 == 0:n = total // 4
else:n = total // 4 + 1
print("Total required %d boats" %(n))

Connected to pydev debugger (build 181.2784.25)
Please input 学生数目: 5
Total required 2 boats

3. 多分支结构

# 多分支结构#1
# 计算分段函数的值。当x大于1时,y=x;当x小于-1时,y=-x;当x介于-1与1之间时,y=1
x = float(input('Please input a number: '))
if x > 1:y=x
elif x <-1:y&#61;-x
else:y&#61;1
print(y)

# 多分支结构#2
# 某校学生成绩采用百分制mark&#xff0c;将其该为五级制度
x &#61; float(input(&#39;Please input a number: &#39;))
if x >100:print(&#39;Error, Please input correct number:&#39;)
elif x >&#61; 90:print("A")
elif x >&#61; 80:print (&#39;B&#39;)
elif x >&#61; 70:print (&#39;B-&#39;)
elif x >&#61; 60:print(&#39;C&#39;)
else:print(&#39;D&#39;)

# 多分支结构#3
# 已知坐标点&#xff08;x,y&#xff09;&#xff0c;判断所在象限
x &#61; float(input(&#39;Please input x: &#39;))
y &#61; float(input(&#39;Please input y: &#39;))
if x&#61;&#61;0 and y&#61;&#61;0:print(&#39;点(%d,%d)在原点:&#39; %(x,y))
elif x&#61;&#61;0 and y!&#61;0:print(&#39;点(%d,%d)y-axis:&#39;%(x,y))
elif x!&#61;0 and y&#61;&#61;0:print(&#39;点(%d,%d)x-axis:&#39;%(x,y))
elif x >0 and y>0:print(&#39;点(%d,%d)第一象限:&#39;%(x,y))
elif x>0 and y<0:print(&#39;点(%d,%d)第4象限:&#39; %(x,y))
elif x <0 and y <0:print(&#39;点(%d,%d)第3象限:&#39; %(x,y))
elif x<0 and y>0:print(&#39;点(%d,%d)第2象限:&#39; %(x,y))
#else:# print(&#39;点(%d,%d)坐标轴上:&#39; %(x,y))

4. 嵌套if语句与逻辑运算

# 判断一年是否为闰年&#xff0c;闰年条件为&#xff1a;年份能被4整除但不能被100整除&#xff0c;或者能被400整除
year &#61; int(input(&#39;Please input a year&#39;))
if year%4 &#61;&#61;0 and year % 100 !&#61;0:print("%d year in run nian"%(year))
elif year%400 &#61;&#61;0:print("%d year in run nian" %(year))
else:print("%d year is not run nian" %(year))

# 判断一年是否为闰年&#xff0c;闰年条件为&#xff1a;年份能被4整除但不能被100整除&#xff0c;或者能被400整除
# 使用逻辑判断
year &#61; int(input(&#39;Please input a year&#39;))
if (year%4 &#61;&#61;0 and year % 100 !&#61;0) or (year % 400 &#61;&#61; 0):print("%d year in run nian"%(year))
else:print("%d year is not run nian" %(year))

等效形式&#xff1a;

if (year%4 &#61;&#61;0 and year % 100 !&#61;0) or (year % 400 &#61;&#61; 0):

if (year%4 &#61;&#61;0 and year % 100) or (year % 400 &#61;&#61; 0):

if ((not(year%4) and year % 100) or year % 400 &#61;&#61; 0):

5. 编写程序

#输出三个数中最大的那个数
a &#61; float(input("Please input 1st number"))
b &#61; float(input("Please input 2nd number"))
c &#61; float(input("Please input 3rd number"))
if (a >&#61; b) and (a >&#61; c):max &#61; aprint(&#39;max number is %s&#39;%(max))
elif (b >&#61; a) and (b >&#61; c):max &#61; bprint(&#39;max number is %s&#39;%(max))
else:max &#61; cprint(&#39;max number is %s&#39; % (max))

#输出三个数中最大的那个数/改进方法2
a &#61; float(input("Please input 1st number"))
b &#61; float(input("Please input 2nd number"))
c &#61; float(input("Please input 3rd number"))
max &#61; a
if (max if (max print(&#39;max number is %s&#39;%(max))

 


推荐阅读
  • 本文介绍了Python函数的定义与调用的方法,以及函数的作用,包括增强代码的可读性和重用性。文章详细解释了函数的定义与调用的语法和规则,以及函数的参数和返回值的用法。同时,还介绍了函数返回值的多种情况和多个值的返回方式。通过学习本文,读者可以更好地理解和使用Python函数,提高代码的可读性和重用性。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了Python异常的捕获、传递与抛出操作,并提供了相关的操作示例。通过异常的捕获和传递,可以有效处理程序中的错误情况。同时,还介绍了如何主动抛出异常。通过本文的学习,读者可以掌握Python中异常处理的基本方法和技巧。 ... [详细]
  • 本文介绍了使用Python根据字典中的值进行排序的方法,并给出了实验结果。通过将字典转化为记录项,可以按照字典中的值进行排序操作。实验结果显示,按照值进行排序后的记录项为[('b', 2), ('a', 3)]。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文介绍了蓝桥训练中的闰年判断问题,并提供了使用Python代码进行判断的方法。根据给定的年份,判断是否为闰年的条件是:年份是4的倍数且不是100的倍数,或者是400的倍数。根据输入的年份,输出结果为yes或no。本文提供了相应的Python代码实现。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • 本文介绍了Python中带有参数的装饰器的概念和使用方法,并提供了装饰器的语法格式和错误写法。同时,还给出了一个加法计算的例子,并展示了执行结果。 ... [详细]
  • 六、流程控制语句
    选择结构if只有条件判断结果为真时才执行相应的操作循环结构for、whileuntil反复执行相同操作时,使用循环结构分支结构case根据变量值的匹配结果执行相 ... [详细]
  • 第1章Python语言概述11.1Python概述11.1.1Python的发展历程11.1.2Python的特点11.1.3Python的应用场合21.2Python的安装21. ... [详细]
  • 流程控制之分支结构
    一. 什么是流程控制流程控制是程序代码执行的顺序。二. 事物执行流程1)顺序结构从上往下依次执行,我们之前所编写的代码都属于该结构2)分支结构事物的 ... [详细]
author-avatar
老邮迢
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有