当我要求Python将数字乘以100时,它会将数字打印100次?

Rya*_*ner 0 python math

例如,当我进入时2 * 100,我得到: 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555

为什么是这样?这是我的代码

import math
KeepProgramRunning = True
while KeepProgramRunning:
    print 'Please enter the centimetre value you wish to convert to millimetres '
    a = raw_input()
    print 'The answer is', 
Run Code Online (Sandbox Code Playgroud)

Ash*_*ary 10

那是因为raw_input()返回一个字符串

使用int()该字符串转换为整数:

a = int(raw_input())
Run Code Online (Sandbox Code Playgroud)

例:

>>> x = raw_input()
2
>>> x * 5
'22222'
>>> x = int(raw_input())
2
>>> x * 5
10
Run Code Online (Sandbox Code Playgroud)


Net*_*ave 7

因为输入检索字符串,请执行以下操作:

import math
KeepProgramRunning = True
while KeepProgramRunning:
    print 'Please enter the centimetre value you wish to convert to millimetres '
    a = int(raw_input())
    print 'The answer is', 
Run Code Online (Sandbox Code Playgroud)

  • 为什么`导入数学'?另外[PEP 8](http://www.python.org/dev/peps/pep-0008/). (4认同)