python - 值错误: could not convert string to float: '8/3'

标签 python input type-conversion

我收到以下说明

x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())

当我尝试以小数值执行它时,例如8/3,我收到一条错误消息。

x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())
0.1 0.2 -8 -16.67 0 0.1 8/3 1
# output
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    x1,y1,x2,y2,x3,y3,xp,yp = map(float, input().split())
ValueError: could not convert string to float: '8/3'

我在这里做错了什么?预先感谢您提供的任何帮助。

最佳答案

8/3 不是 python float 的有效表示法。但是,您可以使用 fractions 解析这些输入模块:

>>> import fractions
>>> inp = '0.1 0.2 8/3 1/2 -5/10'
>>> [float(fractions.Fraction(s)) for s in inp.split()]
[0.1, 0.2, 2.6666666666666665, 0.5, -0.5]

关于python - 值错误: could not convert string to float: '8/3' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74053303/

相关文章:

android - 如何在 Kivy 中重置小部件

python - 将压缩的 xml 提要解析到 ElementTree

javascript - Imask.js 通过在价格末尾附加可变的双零来将货币符号设置为数字的和

c# - 如何将属性从一种类型转换为另一种类型?

type-conversion - Perl6 类属性中的类型强制

python 扭曲: retrieve a deferred's execution time

python - JSON 未正确保存

go - 将 os.Stdin 转换为 []byte

javascript - 这里应该使用 'form',还是没有必要?

c - 如何解决c类型转换中的这个错误?