在python中将浮点数乘以整数时出现数学错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25573298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math error when multiplying float by integer in python
提问by Tom1998
def CalculateExchange(currency2,rate):
currencyamount1 = int(input("Enter the amount: "))
currencyamount2 = (currencyamount1 * rate)
print(currencyamount2,currency2)
When multiplying the exchange rate obtained earlier on in the program by the number inputted by the user, instead of it outputting an actual number, it just outputs the amount entered in the form of the exchange rate, e.g. when the exchange rate is 5 and the amount entered is 6 it will just output 6.6.6.6.6 , I could really use help, I know this problem probably seems quite insignificant and easy to correct but I'm having trouble trying to sort it out.
将程序中先前获得的汇率乘以用户输入的数字时,它不输出实际数字,而是输出以汇率形式输入的金额,例如当汇率为 5 并且输入的数量是 6 它只会输出 6.6.6.6.6 ,我真的可以使用帮助,我知道这个问题可能看起来很微不足道并且很容易纠正,但我在尝试解决它时遇到了麻烦。
采纳答案by asdf
The easiest way to get around an error like this is to cast your int back into a float before multiplying
解决此类错误的最简单方法是在乘法之前将 int 转换回浮点数
def CalculateExchange(currency2,rate):
currencyamount1 = int(input("Enter the amount: "))
currencyamount2 = (float(currencyamount1) * float(rate))
print(currencyamount2,currency2)
回答by TechDude
currencyamount2 = float(currencyamount1 * rate)
回答by dawg
Under Python 2, the function inputperforms an evalon the input string:
在 Python 2 下,函数input对输入字符串执行eval:
Python 2.7.7 (default, Jun 14 2014, 23:12:13)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=input('Enter x: ')
Enter x: 2
>>> x
2
>>> type(x)
<type 'int'>
>>> x*5
10
And a float:
还有一个浮点数:
>>> x=input('Enter x: ')
Enter x: 2.2
>>> type(x)
<type 'float'>
>>> x*5
11.0
Since it is widely considered unwise to take arbitrary code from users in an application, this behavior was changed under Python 3.
由于在应用程序中从用户那里获取任意代码被广泛认为是不明智的,因此这种行为在 Python 3 下发生了变化。
Under Python 3, inputalways returns a string:
在 Python 3 下,输入总是返回一个字符串:
Python 3.4.1 (default, May 19 2014, 13:10:29)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=input('Enter x: ')
Enter x: 2.
>>> type(x)
<class 'str'>
Which explains your result:
这解释了你的结果:
>>> x*5
'2.2.2.2.2.'
If you want to safelyhave similar functionality in Python 3, you can wrap inputin a call to ast.literal_eval:
如果你想在 Python 3 中安全地拥有类似的功能,你可以input调用ast.literal_eval:
>>> from ast import literal_eval
>>> x=literal_eval(input('Enter x: '))
Enter x: 2.2
>>> x
2.2
>>> type(x)
<class 'float'>
Or, just convert the user input to the desired data type with int(x)or float(x)
或者,只需使用int(x)或将用户输入转换为所需的数据类型float(x)

