print(x)TypeError:“ str”对象不可调用

时间:2018-09-23 03:24:19

标签: python python-3.x

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个人可以帮助我吗...

2 个答案:

答案 0 :(得分: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函数时,它引用的是一个字符串,而不是执行您期望的功能。由于字符串不是callable,因此会引发错误。

这是新程序员遇到的常见错误,称为variable shadowing 相反,尝试

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

答案 1 :(得分:0)

发生原因的原因

  

TypeError:“ str”对象不可调用

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

您再次调用print,此后printstr而不是函数。

更改:

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")