为什么此代码返回TypeError:+不支持的操作数类型:“ float”和“ str”?

时间:2018-09-17 21:31:14

标签: python-3.x

lwrnmbr = 0.0
hghrnmbr = 100.0
a = 1

在您实际输入“ h”或“ l”之前,此循环似乎可以正常工作

while (a == 1):
guess = (lwrnmbr + hghrnmbr) / 2
guessword = str (guess)
answr = (input("are you thinking of "+ guessword + "? type 'h' if that's too high, type 'l' if it's too low, and type 'r' if it's right"))
if(answr == "h"): 
    hghrnmbr = answr
elif(answr == "l"):
    lwrnmbr = answr
elif(answr == "r"):
    print("thanks for playing")
    break

1 个答案:

答案 0 :(得分:0)

您不正确地将hghrnmbr / lwrnmbr重新分配为等于您收到的输入,这是我希望您希望将其设置为您所做的猜测的结果。另外,在对数字进行数字运算之前,请确保将guessword转换为整数。

while (a == 1):
guess = (lwrnmbr + hghrnmbr) / 2
guessword = str (guess)
answr = (input("are you thinking of "+ guessword + "? type 'h' if that's too high, type 'l' if it's too low, and type 'r' if it's right"))
if(answr == "h"): 
    hghrnmbr = int(guessword)
elif(answr == "l"):
    lwrnmbr = int(guessword)
elif(answr == "r"):
    print("thanks for playing")
    break