2016-08-02 6 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文の本体の第1ラインにエラーメッセージが構文が無効であることを報告すると、そこにあることを報告します。私はかっこを追加しようとしたが、同じ構文エラーが発生しました。年齢分類子のpythonプログラムは

+1

この場合、入力が「-1」であっても、「Person is an infant」と出力されます。 –

答えて

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

また、論理的なエラーがあります。いずれかの条件が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")