如何使用if和else语句来实现Age Classifier程序

时间:2015-09-11 16:52:14

标签: python if-statement classification

我的Python课程中有一个作业。它说:

  

编写一个程序,要求用户输入一个人的年龄。该   程序应显示一条消息,指示该人是否是   婴儿,儿童,青少年或成人。以下是以下准则:

     
      
  • 如果该人是1岁或以下,他或她是婴儿。
  •   
  • 如果此人年龄超过1岁但未满13岁,则他或她是小孩。
  •   
  • 如果该人年满13岁但不到20岁,则他或她是青少年。
  •   
  • 如果此人年满20岁,则他或她是成年人。
  •   

这是我到目前为止所拥有的。我假设您使用ifelse语句。

age = int(input('Please enter a persons age.'))
if age <= 1: print('The person is an infant.')
else: print('The person is not an infant.')
if age > 1 and age > 13: print('The person is a child.')
else: print ('The person is not an infant.')
if age <= 13 and age > 20: print('The person is a teenager.')
else: print ('The person is not a teenager.')
if age >=20: print ('The person is an adult.')

问题是,当我输入数字,例如数字&#39; 4&#39;时,程序运行如下:

The person is not an infant.
The person is not an infant.
The person is not a teenager.

这就是它给我读的全部内容。那么我该如何解决这个问题呢?因为我以为自己走在了正确的轨道上。

5 个答案:

答案 0 :(得分:0)

使用术语if,elif和else:

查看链式if语句的使用
# ask user to input age
age = int(input('Please enter a persons age.'))

# if a person is 1 or younger
if age <= 1:
    print 'The person is an infant.'
# if a person is older than 1 but younger than 13
elif age > 1 and age < 13:
    print 'The person is a child.'
# if a person is at least 13, but less than 20
elif age >= 13 and age < 20:
    print 'The person is a teenager.'
else if age >= 20:
    print 'The person is an adult.'

看起来你应该从这个练习中学到两点。第一个是如何将if / elif / else语句链接在一起。这样做比单独评估每个条款更有效。如上所示,您可以使用&#39; if&#39;在第一个声明中,&#39; elif&#39;在以下陈述中,&#39;否则&#39;在最后的声明中。

这对于错误检查也很有用。您可以执行以下操作:

# ask user to input age
age = int(input('Please enter a persons age.'))

# if a person is 1 or younger
if age <= 1:
    print 'The person is an infant.'
# if a person is older than 1 but younger than 13
elif age > 1 and age < 13:
    print 'The person is a child.'
# if a person is at least 13, but less than 20
elif age >= 13 and age < 20:
    print 'The person is a teenager.'
elif if age >= 20:
    print 'The person is an adult.'
else:
    print 'Check that your input is an integer and try again.'

第二个是&lt; &GT;和&lt; =&gt; =。请注意,如果他们未满1岁,则该人将其分类为婴儿。这不包括年龄为1岁,仅包括尚未达到1岁的人,因此您将使用&#39;&lt; 1&#39;获得长达364天的一切。

这与将某人至少归类为&#39; 13岁时少年。这意味着它们可以是13或更高,因此您将使用&#39;&gt; = 13&#39;。

答案 1 :(得分:0)

似乎你陷入了一个循环,你需要告诉程序要说什么,例如。 如果年龄<= 1: 打印(“你是个婴儿”) 其他:     打印(“你还是个孩子”)

答案 2 :(得分:0)

# It will work In sha allah!
age = int(input('Please enter a persons age.'))

# if a person is 1 or younger
if age <= 1:
    print ("The person is an infant")
# if a person is older than 1 but younger or same as 13
elif age > 1 and age <= 13:
    print ("The person is a child")
# if a person is at least 13, but less than 20
elif age >= 13 and age < 20:
    print ("The person is a teenager")
elif  age >= 20:
    print ("The person is an adult")
else:
    print ("wrong age")

答案 3 :(得分:-1)

我希望这能解决您的问题

print "Enter Age"
age = input()
if age<1:
  print "he or she is an infant."
elif age>=1 and age<13:
  print "he or she is a child"
elif age>=13 and age<20:
  print "He or she is an adult"
elif age>=20:
  print "he or she is an adult"
else:
  print "adult"

答案 4 :(得分:-1)

for i in range(5):
    age = int(input('Please enter a persons age.'))
    if age <= 1:                  
        print(f'The person is an infant.')

    elif age > 1 and age < 13:
        print('The person is a child.')

    elif age >= 13 and age < 20:
        print('The person is a teenager.')

    elif age >= 20:
        print('The person is an adult.')

    else:
        print("wrong age")