2016-08-02 86 views
1
print("Welcome to the Age Classifier program") 
person_age=(float(input("Enter the person's Age")) 

if person_age<=1 or person_age>0: 
     print("Person is an infant") 
elif person_age>1 or person_age<13: 
     print("Person is a child") 
elif person_age>=13 or person_age<20: 
     print("Person is a teenager") 

elif person_age>=20 : 
     print("Person is an adult") 
else: 
     print("Person has not been conceived or is developing in the womb") 

当我执行这段代码,解释报告说,没有对if陈述的身体一号线错误,有消息报道说语法是无效的。我尝试添加括号并遇到相同的语法错误。年龄分类的Python程序

+1

在这种情况下,即使输入为'-1',输出也会是''人是婴儿''。 –

回答

1

你有不平衡的括号。

person_age=float(input("Enter the person's Age")) 

这可能会是一个更好的主意,不过,使这是一个整数

person_age=int(input("Enter the person's Age")) 
+2

谢谢,显然漂浮不采取负数 –

+0

很高兴我们可以提供帮助。 –

2

在第一行中的错误主要是由于括号:

person_age=(float(input("Enter the person's Age")) # 3 opening, 2 closing. 

将其更改为:

person_age=(float(input("Enter the person's Age"))) 

另外,你有一个逻辑错误。如果任一条件为真,则or运算符返回True。我怀疑这是否适合你的用例。你应该这样做:

if person_age<=1 and person_age>0: 
     print("Person is an infant") 
elif person_age>1 and person_age<13: 
     print("Person is a child") 
elif person_age>=13 and person_age<20: 
     print("Person is a teenager") 
elif person_age>=20 : 
     print("Person is an adult") 
else: 
     print("Person has not been conceived or is developing in the womb")