Python 类型错误>;=在';str';和';int';

Python 类型错误>;=在';str';和';int';,python,Python,我已经写出了我的代码,但如果personsAge>=1,我会在上不断收到错误消息 以下是错误: 类型错误>=在'str'和'int'的实例之间不受支持 每次我运行程序时都会发生这种情况。我不确定我做错了什么。非常感谢您的帮助 这是我的密码: # Purpose ''' This program classifies a person to a certain group based on their age''' #========================================

我已经写出了我的代码,但如果personsAge>=1,我会在
上不断收到错误消息

以下是错误:

类型错误>=在'str'和'int'的实例之间不受支持

每次我运行程序时都会发生这种情况。我不确定我做错了什么。非常感谢您的帮助

这是我的密码:

# Purpose
''' This program classifies a person to a certain group based on their age'''
#=================================================================================
# Initialize variables(used for processing data)
ageGroup =""; #specify data as empty string
personsAge =""; #specify data as float /real
#====================================================================================

# Input Statements
fullName = str(input("<Enter your full name>"));
personsAge = str(input("<Enter your age>"));

#==========================================================================
'''nested if statements to determine a person's age group.'''

# Using the Nested If eliminates the need to check all if statements
# Once the criteria has been met, control is transferred to the statement
# following the last if Statement. However, the IF/ELSE must be aligned in same column

if (personsAge <=1) and (personsAge >0):
    ageGroup = "Infant.";

elif (personsAge >=1) and (personsAge <=13):
    ageGroup = "Child.";

elif (personsAge >=13) and (personsAge <=20):
    ageGroup = "Teenager.";

elif (personsAge >=20):
    ageGroup = "Adult.";
#====================================================================================
#Output Statements
print();
print("My name is " + fullName);
print("My age is " + personsAge);

#=================================================================
# Print Age group
print(fullName + "is an " + ageGroup);
print("=" * 80);
#===============================================================================
#End program
#目的
“该程序根据年龄将一个人划分为特定的群体”
#=================================================================================
#初始化变量(用于处理数据)
年龄组=”#将数据指定为空字符串
personsAge=“”#将数据指定为浮点/实数
#====================================================================================
#输入语句
fullName=str(输入(“”);
personsAge=str(输入(“”);
#==========================================================================
''确定一个人年龄组的嵌套if语句''
#使用嵌套的If无需检查所有If语句
#一旦满足标准,控制权就转移到语句中
#在最后一个if语句之后。但是,IF/ELSE必须在同一列中对齐
如果(人员0):
年龄组=“婴儿。”;
elif(人名>=1)和(人名=13)以及(人名=20):
年龄组=“成人。”;
#====================================================================================
#输出语句
打印();
打印(“我的名字是”+全名);
打印(“我的年龄是”+个人年龄);
#=================================================================
#打印年龄组
打印(全名+”为“+年龄组);
打印(“=”*80);
#===============================================================================
#结束程序

您正在将输入转换为字符串而不是整数

使用
int()
将输入转换为整数:

personsAge = int(input("<Enter your age>"));

在不能保证用户输入为所需类型的情况下,添加错误处理是一种很好的做法。例如:

while True:
    try:
        personsAge = int(input("<Enter your age>"))
        break
    except ValueError:
        print('Please input an integer.')
        continue
为True时:
尝试:
personsAge=int(输入(“”)
打破
除值错误外:
打印('请输入一个整数')
持续

您正在将输入转换为字符串而不是整数

使用
int()
将输入转换为整数:

personsAge = int(input("<Enter your age>"));

在不能保证用户输入为所需类型的情况下,添加错误处理是一种很好的做法。例如:

while True:
    try:
        personsAge = int(input("<Enter your age>"))
        break
    except ValueError:
        print('Please input an integer.')
        continue
为True时:
尝试:
personsAge=int(输入(“”)
打破
除值错误外:
打印('请输入一个整数')
持续

您需要将输入转换为整数:
int(输入(“”)
。您应该在谷歌上搜索这个错误。这个问题每天至少被问一次。很抱歉,我是一个初学者coding@ChristianDean谢谢他们破坏了Android应用程序。如果我切换到浏览器查找复制品,我无法切换回应用程序。我需要杀掉所有东西,重新装填。非常烦人。是的,这就是我一般不在手机上做太多堆栈溢出的原因之一:p你需要将输入转换为整数:
int(input(“”)
。你应该在谷歌上搜索这个错误。这个问题每天至少被问一次。很抱歉,我是一个初学者coding@ChristianDean谢谢他们破坏了Android应用程序。如果我切换到浏览器查找复制品,我无法切换回应用程序。我需要杀掉所有东西,重新装填。非常烦人。是的,这就是我一般不在手机上做太多堆栈溢出的原因之一:PI将我的输入值随个人年龄更改为int,但现在当我运行它时,我得到类型错误:必须不是strint@Andrew您需要更新输出语句。既然
personsAge
是一个整数,那么每次尝试将其与字符串连接时,都应该使用
str()
将其转换为字符串。我相应地编辑了我的答案。我将输入的persons age更改为int,但现在当我运行它时,我得到了类型错误:必须是str notint@Andrew您需要更新输出语句。既然
personsAge
是一个整数,那么每次尝试将其与字符串连接时,都应该使用
str()
将其转换为字符串。我据此编辑了我的答案。