-1
import random 
print("guass a number in between 0 t0 10")
x=random.randint(0,10)
print(x)
while True:
    print=("if i have to think lower press 1,if i have to think higher press 2,if correct press 3")
    y=int(input("press your choice"))
    if y==1:
        x=random.randint(0,x)
        print(x)
    elif y==2:
        x=random .randint(x,10)
        print(x)
    elif y==3:
        print("thank you")
    else:
        print("chosee a valid no")
        break

任何1可以帮助我......

4

2 回答 2

1

在您的第一次迭代中,您while-loop初始化一个以值命名print的变量"if i have to think lower press 1,if i have to think higher press 2,if correct press 3"

我不认为你打算这样做。

print=("if i have to think lower press 1,if i have to think higher press 2,if correct press 3")

当您这样做并尝试调用该print函数时,它不是按照您的期望执行的,而是引用 a 字符串。由于字符串不是可调用的,因此会引发错误。

对于新程序员来说,这是一个常见的错误,称为变量阴影 而不是尝试,

print("if i have to think lower press 1,if i have to think higher press 2,if correct press 3")
于 2018-09-23T03:32:21.310 回答
0

你出现的原因

TypeError:“str”对象不可调用

是打印=(“如果我必须考虑按1,如果我必须考虑按2,如果正确按3”)`。

你再调用print一次,从此print是一个str而不是一个函数。

改变:

print=("if i have to think lower press 1,if i have to think higher press 2,if correct press 3")

print ("if i have to think lower press 1,if i have to think higher press 2,if correct press 3")
于 2018-09-23T06:27:08.203 回答