简体   繁体   English

我在这里收到错误错误是('str' object 不可调用)

[英]I am getting errors here error is ('str' object is not callable)

print("Lets play a game")
print("You have to operte on some numbers")

print("Are you ready? , type yes or no ")

input = input().lower()

if input == "yes":
    print("Ok now type your name and age (comma separated)")
    name = input()
    age = int(input())
    if "\"" in name or ">" in name :
        print("Use only alphanumeric characters")
    else:
        pass
    if age <= 10 :
        print("You cant play this game")

I keep on getting this error 'str' object is not callable in line 10.我不断收到此错误'str' object is not callable在第 10 行。

You are shadowing the built-in input() function with the input variable, so the second time you call input() it tries to 'call' the string variable.您正在使用input变量隐藏内置input() function,因此第二次调用input()它会尝试“调用”字符串变量。

You will have to work out what to do when the user doesn't input what you expect, but here's a skeleton for what the code could look like:当用户没有输入您期望的内容时,您必须弄清楚该怎么做,但这里有一个代码可能看起来像这样的框架:

print("Let's play a game")
print("You have to operate on some numbers")
text = input("Are you ready (yes/no)?").lower()

if text == "yes":
    text = input("Enter your name and age (comma separated):")
    fields = text.split(",")
    if len(fields) != 2:
        # Handle case of bad user input format

    name = fields[0].trim()
    if not all(c.isalpha() or c.isspace() for c in name):
        # Handle case of bad entered name
    try:
        age = int(fields[1])
    except ValueError:
        # Handle case of bad entered age

    if age <= 10:
        print("You cannot play this game")

The problem you are getting derives from the name of the variable, "input".您遇到的问题来自变量的名称“输入”。 If you change it, "ready" in my example, you will avoid the error.如果您更改它,在我的示例中为“准备好”,您将避免该错误。

I would also recommend to include functions to check the inputs and use the while loop, so the program does not finish if there is an unexpected input.我还建议包含检查输入的函数并使用 while 循环,这样如果出现意外输入,程序就不会完成。

Please, see my code below:请看下面我的代码:

def check_predefined_input(variable, values, input_question, error_message, check = False):
    variable = str(input(input_question))
    try:
        if(variable.lower() in values):
            check = True
        else:
            raise Exception ("You have inserted an invalid option.")
    except Exception as e:
        print("ERROR: ", e)
        print(error_message, "\n")
        check = False
    return variable, check

def check_string_input(variable, input_question, error_message, check = False):
    variable = input(input_question)
    if "\"" in variable or ">" in variable:
        print("ERROR: ", error_message, "\n")
        check = False
    else:
        check = True
    return variable, check

def check_int_input(variable, input_question, error_message, check = False):
    try:
        variable = int(input(input_question))
        check = True
    except ValueError as ve:
        print("ERROR: ", ve)
        print(error_message, "\n")
        variable = 0
        check = False
    return variable, check

print("Lets play a game")
print("You have to operate on some numbers")

ready_values = ["yes", "no"]
ready = ''
ready_input_question = "Are you ready? , type yes or no "
ready_error_message =  "\tMake sure your answer is yes or not."
ready_check = False
while not ready_check:
    ready, ready_check = check_predefined_input(ready, ready_values, ready_input_question, ready_error_message, ready_check)


if ready == "yes":
    name = ""
    name_check = False
    name_input_question = "Ok now type your name: "
    name_error_message = "Use only alphanumeric characters"
    while not name_check:
        name, name_check = check_string_input(name, name_input_question, name_error_message, name_check)

    age = 0
    age_check = False
    age_input_question = 'Please, ' + name + ', insert your age: '
    age_error_message =  '\tMake sure you insert your age.\n\tDo not insert letters.'
    while not age_check:
        age, age_check= check_int_input(age, age_input_question, age_error_message)

    if age <= 10 :
        print("You cannot play this game")
    else:
        print("You are ready to play")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到“str”objet is not callable 错误? - Why am I getting a 'str' objet is not callable error? 我收到“元组”对象无法作为错误调用 - I am getting 'tuple' object is not callable as an error 在 Python 中出现错误 `SyntaxWarning: 'str' object is not callable` - Getting error `SyntaxWarning: 'str' object is not callable` in Python TypeError: &#39;str&#39; object is not callable 但我没有覆盖 str 方法? - TypeError: 'str' object is not callable but I am not overwriting the str method? 当我运行此命令时,我收到TypeError:&#39;str&#39;对象不可调用..为什么呢? - When I run this , i am getting TypeError: 'str' object is not callable.. why is that? 我不断收到 TypeError: 'str' object is not callable 我不知道如何修复它 - I keep getting TypeError: 'str' object is not callable and I am not sure how to fix it 为什么我在Python中得到“&#39;tuple&#39;对象不是可调用错误” - Why am I getting "'tuple' object is not callable error in python TypeError: 'Tk' object is not callable 为什么我会收到此错误消息 - TypeError: 'Tk' object is not callable Why am i Getting this error message 为什么我收到 TypeError: 'str' object is not callable when using eval() on string that is python code - Why am I getting a TypeError: 'str' object is not callable when using eval() on string that is python code 为什么我在 python 上得到一个列表 object is not callable 错误? - Why am I getting a list object is not callable error on python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM