字符串和True / False

时间:2017-08-04 11:04:11

标签: python

def check_guess(letter, guess):
    if letter == guess:
        result = "You are correct"
    elif letter < guess:
        result = "It is too low" + letter == guess
    elif letter > guess:
        result = "It is too high" + letter == guess        
    else:
        result = "It is an invalid input " + letter == guess
    return result

print (check_guess(raw_input("What is your guess (lower case) ?"), raw_input("Please input one lower case alpha! ")))

我想知道为什么我不能显示字符串&#34;它太低&#34;在解决方案?

3 个答案:

答案 0 :(得分:0)

使用布尔结果添加字符串会导致问题。 尝试

result = "It is too low" + str(letter == guess)  

这应该可以解决问题。也正如其他人指出的那样,在您的问题中发布代码,而不仅仅是图片。

更好的解决方案可以是:

result = "It is too low %s" %(letter == guess)

Check this question

答案 1 :(得分:0)

你没有指定你想要显示的内容,所以要么像他们的回答中所说的那样:

result = "It is too low %s" %(letter == guess)

给出

&#34;错误太低&#34;

result = "It is too low {} == {}".format(letter, guess)

给出

&#34;它太低了= = b&#34;

如果字母=&#39; a&#39;并猜测=&#39; b&#39;

答案 2 :(得分:0)

是否有必要在letter声明中检查guess是否等于if?如果没有,您只需返回字符串即可。希望你明白这一点。

def check_guess(letter, guess):
    if letter < guess:
        return 'It is too low'

print(check_guess('a', 'b'))