Python中的乘法无法正常工作 [英] Multiplication in Python not working
问题描述
我在python 3.4中遇到了一个奇怪的错误,即乘法不起作用!
I have the bizarre error in python 3.4 where multiplication does not work!
这是我的代码:
timerlenth = input('Please enter the amount of minute: ')
int(timerlenth)
timersec = (timerlenth*60)
print (timersec)
以下是结果的照片:
我对解决问题几乎一无所知!
I am practically clueless on trying to solve the problem!
推荐答案
input
函数返回一个字符串.因此,变量 timerlenth
存储一个字符串.在下一行, int(timerlenth)
将此变量转换为整数,但对结果不执行任何操作,将 timerlenth
保留为以前的字符串.Python具有此功能,其中 [string] * x
将重复字符串 x
次,这就是您在输出中看到的内容.
The input
function returns a string. Therefore the variable timerlenth
stores a string. Next line, int(timerlenth)
converts this variable to integer, but does nothing with the result, leaving the timerlenth
as the same string it used to be. Python has this functionality where [string]*x
will repeat the string x
times and that's what you see in the output.
要获得实际的乘法,您必须将 int(timerlenth)
的值存储到一个变量中,最好是一个新变量(良好的编程习惯),并将新值与乘法运算一起使用.
To get actual multiplication, you'll have to store the value of int(timerlenth)
to a variable, preferably a new one (good programming practice) and use the new value with multiplication operation.
这篇关于Python中的乘法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!