2017-10-20 142 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())