我收到以下说明
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/