Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2:ValueError:int()的文本无效,以10为底:';40.0';_Python_Math_Python 2.7 - Fatal编程技术网

Python 2:ValueError:int()的文本无效,以10为底:';40.0';

Python 2:ValueError:int()的文本无效,以10为底:';40.0';,python,math,python-2.7,Python,Math,Python 2.7,我有几个变量,x,y和结果: >>>x = 40 >>>y = 45 >>>result = x / y * 100 >>>result 0 所以result==0。我知道result==0,因为我必须打印x=40.0,而不是打印x=40 但无论如何,我需要它打印出来: 88.88888888888889 我该怎么做 注意:我不能只写x=40.0 我也试着这样做: x1 = str(x) + '.0' result =

我有几个变量,
x
y
结果

>>>x = 40
>>>y = 45
>>>result = x / y * 100
>>>result
0
所以
result==0
。我知道
result==0
,因为我必须打印
x=40.0
,而不是打印
x=40

但无论如何,我需要它打印出来:

88.88888888888889
我该怎么做

注意:我不能只写
x=40.0

我也试着这样做:

x1 = str(x) + '.0'
result = int(x1) / y * 100
但是Python给了我一个错误:

ValueError: invalid literal for int() with base 10: '40.0'
那么我该如何解决这个问题呢

>>> from __future__ import division
>>> x = 40
>>> y = 45
>>> result = x / y * 100
>>> result
88.88888888888889
在Python2.7中,使用整型参数执行
/
。您可以将其更改为在导入时使用标准分割,如图所示

或者(如果不希望以这种方式更改
/
的行为),可以将其中一个除法参数设置为浮点:
float(x)/y*100