Python 我在这里遇到错误错误错误是(';str';对象不可调用) print(“让我们玩一个游戏”) 打印(“您必须对某些数字进行操作”) 打印(“准备好了吗?键入是或否”) 输入=输入() 如果输入=“是”: 打印(“确定现在键入您的姓名和年龄(逗号分隔)”) 名称=输入() 年龄=整数(输入() 如果名称中有“\”或“>”: 打印(“仅使用字母数字字符”) 其他: 通过 如果使用input变量隐藏内置的input()函数,那么第二次调用input()时,它会尝试“调用”字符串变量

Python 我在这里遇到错误错误错误是(';str';对象不可调用) print(“让我们玩一个游戏”) 打印(“您必须对某些数字进行操作”) 打印(“准备好了吗?键入是或否”) 输入=输入() 如果输入=“是”: 打印(“确定现在键入您的姓名和年龄(逗号分隔)”) 名称=输入() 年龄=整数(输入() 如果名称中有“\”或“>”: 打印(“仅使用字母数字字符”) 其他: 通过 如果使用input变量隐藏内置的input()函数,那么第二次调用input()时,它会尝试“调用”字符串变量,python,Python,当用户没有输入您期望的内容时,您必须确定该怎么做,但下面是代码的框架: 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 separate

当用户没有输入您期望的内容时,您必须确定该怎么做,但下面是代码的框架:

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

print(“让我们玩一个游戏”)
打印(“您必须对某些数字进行操作”)
text=输入(“您准备好了吗(是/否)”)。lower()
如果文本==“是”:
text=输入(“输入您的姓名和年龄(逗号分隔):”)
字段=文本。拆分(“,”)
如果len(字段)!=2:
#处理用户输入格式错误的情况
名称=字段[0]。修剪()
如果不是全部(c.isalpha()或c.isspace()表示名称中的c):
#输入不良名称的处理
尝试:
年龄=整数(字段[1])
除值错误外:
#处理不良输入年龄的情况

如果您遇到的问题来自变量名“input”。在我的示例中,如果您将其更改为“ready”,您将避免错误

我还建议包含检查输入的函数,并使用while循环,以便在出现意外输入时程序不会完成

请参阅下面我的代码:

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")
def check\u预定义的输入(变量、值、输入问题、错误消息、check=False):
变量=str(输入(输入问题))
尝试:
如果(值中的变量.lower()):
检查=正确
其他:
引发异常(“您插入了无效选项”)
例外情况除外,如e:
打印(“错误:”,e)
打印(错误消息“\n”)
检查=错误
返回变量,检查
def检查字符串输入(变量、输入问题、错误消息、检查=False):
变量=输入(输入\问题)
如果变量中有“\”或变量中有“>”:
打印(“错误:”,错误消息“\n”)
检查=错误
其他:
检查=正确
返回变量,检查
def检查输入(变量、输入问题、错误消息、检查=False):
尝试:
变量=int(输入(输入问题))
检查=正确
除ValueError为ve外:
打印(“错误:”,ve)
打印(错误消息“\n”)
变量=0
检查=错误
返回变量,检查
打印(“让我们玩一个游戏”)
打印(“您必须对某些数字进行操作”)
就绪值=[“是”、“否”]
就绪=“”
就绪\u输入\u question=“准备好了吗?”键入是或否
ready\u error\u message=“\t确保您的答案是肯定还是否定。”
就绪检查=错误
未准备就绪时,请检查:
就绪,就绪\u检查=检查预定义的\u输入(就绪,就绪\u值,就绪\u输入\u问题,就绪\u错误消息,就绪\u检查)
如果就绪==“是”:
name=“”
name\u check=False
name\u input\u question=“确定现在键入您的姓名:”
name\u error\u message=“仅使用字母数字字符”
而不是名称检查:
名称,名称检查=检查字符串输入(名称,名称输入问题,名称错误消息,名称检查)
年龄=0
年龄检查=假
年龄输入问题='请'+姓名+',插入您的年龄:'
年龄\错误\消息='\t请确保插入年龄。\n\t不要插入字母。'
而不是年龄检查:
年龄,年龄检查=检查输入(年龄,年龄输入问题,年龄错误信息)

如果age
input=input()
-您正在跟踪函数。不要将一个名称用于两件事。使用
input=input().lower()
时,会覆盖
input
函数的内置名称。这意味着,在运行该行之前,
input
是一个内置函数,之后是一个字符串。稍后,当您再次尝试运行
input()
时,会抛出此错误,请更改您的变量名(例如
input\u val=input().lower()
)现在我明白了,谢谢:)有人知道如何将我的“age”变量设为整数?与名称类似,age=input().split(“,”)?
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")