2012-10-10 36 views
0

例如,當我進入2 * 100,我得到: 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555當我要求Python將數字乘以100時,它會打印數字100次?

這是爲什麼? 這裏是我的代碼

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

我什麼也看不到有需要的'math'模塊 – tMC

+0

我知道,變量名是主觀的,但KeepProgramRunning簡直是可怕..只是使用在運行: – Ant

回答

10

這是因爲raw_input()返回一個字符串

使用int()該字符串轉換爲整數:

a = int(raw_input()) 

例如:

>>> x = raw_input() 
2 
>>> x * 5 
'22222' 
>>> x = int(raw_input()) 
2 
>>> x * 5 
10 
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', 
+4

爲什麼'進口math'?另外[PEP 8](http://www.python.org/dev/peps/pep-0008/)。 – delnan

+0

我剛剛檢查了上面的代碼,可能他需要它來做其他事情。 – Netwave

相關問題