Age Classifier python程序

时间:2016-08-02 19:47:02

标签: python syntax

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语句正文的第1行有错误,并显示语法无效的消息。我尝试添加括号,遇到相同的语法错误。

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

此外,您有逻辑错误。如果其中一个条件为True,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")

答案 1 :(得分:1)

你的括号不平衡。

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

但是,这可能是一个更好的想法,使它成为一个整数:

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