2017-10-20 166 views
-2

我不知道爲什麼這個代碼不打印「得到了它」 當我運行它,它只是表明我打印else語句,即使答案是正確的。if語句,並打印在Python

import random as rand 
print('Welcome to the guessing game!') 
print('type a number between 1 to 9') 
running = True 
while running: 
    value = rand.randint(1, 9) 
    user_guess = input() 

    if user_guess == value: 
     print('got it') 
    else: 
     print('not at all') 

即使嘗試打印值,以確保我的答案是正確的。

+1

嘗試'打印(類型(user_guess),類型(值))' – Kevin

+0

不明白 –

+0

'STR!= int',它永遠不會......你將需要轉換'user_guess ''到的int' [我怎樣才能讀取輸入的整數?] – abccd

回答

0

由於後

user_guess = input() 

user_guess將是一個strvalueint
在聲明if user_guess == value:你試圖用int比較str
嘗試

user_guess = int(input())