我正在接受后续的指导
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'我在这里做错什么了?提前感谢您的任何帮助。
发布于 2022-10-13 09:25:31
8/3不是python浮点数的有效表示法。但是,您可以使用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]发布于 2022-10-13 09:26:04
字符串8/3不能使用float转换,只需使用fractions模块中的一些内容即可。
https://stackoverflow.com/questions/74053303
复制相似问题