我是靠谱客的博主 美满电话,这篇文章主要介绍[Python] If语句练习题,现在分享给大家,希望可以做个参考。

目录

一、输入三个整数x,y,z,请把这三个数由小到大输出

方法1:确定最大值和最小值做比较(比较繁琐)

方法2:通过两两比较重新赋值,确定最小值

方法3:使用排列组合的方式连续判断

二、依次输入三角形的三边长,判断能否生成一个三角形

方法1:确定最长边,判断(三边总长-最长边)与最长边的关系

方法2:直接用两边之和与第三边关系判断

方法3:通过两两比较重新赋值,确定最长边

三、依次输入三角形的三边长,判断能否生成一个等腰三角形

方法1:通过计算

方法2:用or连接三种可能性

四、依次输入三角形的三边长,判断能否生成一个直角三角形

方法1:多计算

方法2:通过重新赋值确定最长边



一、输入三个整数x,y,z,请把这三个数由小到大输出

方法1:确定最大值和最小值做比较(比较繁琐)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x = int(input("请输入x=")) y = int(input("请输入y=")) z = int(input("请输入z=")) if min(x,y,z) ==x and max(x,y,z)==z: print(x,y,z) elif min(x,y,z) ==x and max(x,y,z)==y: print(x,z,y) elif min(x, y, z) == z and max(x, y, z) == x: print(z, y, x) elif min(x, y, z) == z and max(x, y, z) == y: print(z, x, y) elif min(x, y, z) == y and max(x, y, z) == x: print(y, z, x) else: print(y,x,z)

方法2:通过两两比较重新赋值,确定最小值

复制代码
1
2
3
4
5
6
7
8
9
10
11
x = int(input("请输入x=")) y = int(input("请输入y=")) z = int(input("请输入z=")) if x>y: x,y=y,x if x>z: x,z=z,x if y>z: y,z=z,y print("{}<{}<{}".format(x,y,z))

方法3:使用排列组合的方式连续判断

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x = int(input("请输入x=")) y = int(input("请输入y=")) z = int(input("请输入z=")) if(x>y>z): print(z,y,x) if(x>z>y): print(y,z,x) if(y>x>z): print(z,x,y) if(y>z>x): print(x,z,y) if(z>x>y): print(y,x,z) if (z>y>x): print(x,y,z)

二、依次输入三角形的三边长,判断能否生成一个三角形

方法1:确定最长边,判断(三边总长-最长边)与最长边的关系

复制代码
1
2
3
4
5
6
7
8
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if max(a,b,c)< a+b+c-max(a,b,c): print("可以生成三角形") else: print("不可以生成三角形")

方法2:直接用两边之和与第三边关系判断

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if max(a,b,c)==c and a+b>c: print("可以生成三角形") elif max(a,b,c)==b and a+c>b: print("可以生成三角形") elif max(a,b,c)==a and b+c>a: print("可以生成三角形") else: print("不可以生成三角形")

方法3:通过两两比较重新赋值,确定最长边

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if a<b: a,b=b,a if a<c: a,c=c,a if a<b+c: print("可以生成三角形") else: print("不可以生成三角形")

三、依次输入三角形的三边长,判断能否生成一个等腰三角形

方法1:通过计算

复制代码
1
2
3
4
5
6
7
8
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if min(a,b,c)== (a+b+c-max(a,b,c))/2 and max(a,b,c)< min(a,b,c)*2: print("可以生成等腰三角形") else: print("不可以生成等腰三角形")

方法2:用or连接三种可能性

复制代码
1
2
3
4
5
6
7
8
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if((a==b and a+b>c)or(a==c and a+c>b) or (b==c and b+c>a) ): print("可以生成等腰三角形") else: print("不可以生成等腰三角形")

四、依次输入三角形的三边长,判断能否生成一个直角三角形

方法1:多计算

复制代码
1
2
3
4
5
6
7
8
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if max(a,b,c)< a+b+c-max(a,b,c) and max(a,b,c)**2==min(a,b,c)**2+(a+b+c-max(a,b,c)-min(a,b,c))**2: print("可以生成直角三角形") else: print("不可以生成直角三角形")

方法2:通过重新赋值确定最长边

复制代码
1
2
3
4
5
6
7
8
9
10
a = int(input("请输入边长a=")) b = int(input("请输入边长b=")) c = int(input("请输入边长c=")) if a<b: a,b=b,a if a<c: a,c=c,a if(a**2==b**2+c**2): print("可以生成直角三角形")

最后

以上就是美满电话最近收集整理的关于[Python] If语句练习题的全部内容,更多相关[Python]内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部