Python 3.x Python中的乘法不起作用
我在Python3.4中遇到了一个奇怪的错误,乘法不起作用 这是我的代码:Python 3.x Python中的乘法不起作用,python-3.x,math,Python 3.x,Math,我在Python3.4中遇到了一个奇怪的错误,乘法不起作用 这是我的代码: timerlenth = input('Please enter the amount of minute: ') int(timerlenth) timersec = (timerlenth*60) print (timersec) 以下是结果的照片: 在试图解决这个问题上,我几乎一无所知 timerlenth是一个字符串,因此*操作符只需将其压缩60次,而不是将其相乘。这是由于误用了int——它不会更改传递的参数
timerlenth = input('Please enter the amount of minute: ')
int(timerlenth)
timersec = (timerlenth*60)
print (timersec)
以下是结果的照片:
在试图解决这个问题上,我几乎一无所知
timerlenth
是一个字符串,因此*
操作符只需将其压缩60次,而不是将其相乘。这是由于误用了int
——它不会更改传递的参数,而是为其返回一个整数值,如果不在任何位置赋值,则会丢失整数值。只需将其重新分配到timerlenth
即可:
timerlenth = int(timerlenth)
input
函数返回一个字符串。因此,变量timerlenth
存储字符串。下一行,int(timerlenth)
将此变量转换为整数,但对结果不做任何处理,将timerlenth
保留为以前的字符串。Python有这个功能,[string]*x
将重复字符串x
次,这就是您在输出中看到的
要获得实际的乘法运算,必须将int(timerlenth)
的值存储到一个变量中,最好是一个新变量(良好的编程实践),并将新值与乘法运算一起使用