python - 如何分离这些输入并仍然使它们成为浮点数?
问题描述
三角形可以根据其边长分类为等边、等腰或不等边。等边三角形的所有 3 条边都具有相同的长度。等腰三角形有两条边长度相同,第三条边长度不同。如果所有边的长度不同,则三角形是不等边的。编写一个程序,从用户那里读取三角形 3 条边的长度。显示指示三角形类型的消息
这是我的代码,它不起作用:
#this code doesn't work for some reason:
s1, s2, s3 = float(input('Enter three sides (separated by comma): ').split(','))
if s1 == s2 and s2 == s3:
print('Equilateral')
elif (s1 == s2 and s2 != s3) or (s1 == s3 and s2 != s3) or (s2 == s3 and s1 != s2):
print('Isosceles')
else:
print('Scalene')
我错过了什么?
解决方案
这看起来像您正在尝试将字符串列表转换为浮点列表。这不起作用。
我会使用这样的东西:
str1, str2, str3 = input('Enter three sides (separated by comma): ').split(',')
s1 = float(str1)
s2 = float(str2)
s3 = float(str3)
或列表理解:
s1, s2, s3 = [float(s) for s in input('Enter three sides (separated by comma): ').split(',')]
推荐阅读
- reactjs - ExcelJs 使用 typescript 添加常用样式功能
- c# - 值不能为空。(参数'connectionString')第一次运行迁移时
- swift - 应用转移iOS Swift后用户是否需要重新安装我的应用
- video-streaming - Playready 许可证的标准生命周期
- amazon-web-services - 如何使用 eb 的 cli 工具将部署策略更新为不可变?
- r - 基础问题:如何使用 R 将列表向量转换为表格?
- swift - 将更改发布到可观察对象的集合
- php - Woocommerce 5.4.1:如何将 Hook 添加到存档页面上的特定帖子 - 商店页面
- python - Julia 中一维随机游走的直方图
- javascript - WSO2 EI - <(小于符号)在脚本中介(Javascript)中抛出错误