2017-06-21 24 views
0

當我運行這個程序爲什麼隨機整數的上限不是20? (Python 3中)

print('Welcome to the algebra machine!') 
from random import * 
a=randint(1,20) 
b=(input('Choose a number:')) 
c=a*b 
print('You got a total of',c) 
d=(input('What was the original number?')) 
if d==a: 
    print('Correct!') 
else: 
    print('Wrong!') 

我會得到的最終答案,例如,如果22222222我進入2。 爲什麼不randint上限爲20

+2

要添加到答案:你可以使用到Python 2.x的,其中'輸入()'函數的輸入轉換爲它的默認類型。然而,在Python 3.x中,輸入仍然是一個字符串。如果你想要另一種類型,程序需要明確地進行轉換。 –

回答

2

上限爲20.問題是您將輸入號碼保留爲字符串。您發佈的輸出22222222是8 *'2'。如果你想看到16的輸出,儘量

b = int(input('Choose a number:')) 
0

b是一個字符串,並且任何時候你通過一個字符串乘以一個數字,它重複字符串n倍。

"2" * 5回報"22222"

轉換b了一些嘗試做就可以了數學之前。

相關問題