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

时间:2012-10-10 18:11:36

标签: 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', 

2 个答案:

答案 0 :(得分:10)

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

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

a = int(raw_input())

示例:

>>> x = raw_input()
2
>>> x * 5
'22222'
>>> x = int(raw_input())
2
>>> x * 5
10

答案 1 :(得分: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',