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

How to use if and else statements to achieve Age Classifier program

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

Write a program that asks the user to enter a person's age. The program should display a message indicating whether the person is an infant, child, teenager, or adult. Here are the following guidelines:

  • If the person is 1 year old or less, he or she is an infant.
  • If the person is older than 1 year, but younger than 13 years, he or she is a child.
  • If the person is at least 13 years old, but less than 20 years old, he or she is a teenager.
  • If the person is at least 20 years old, he or she is an adult.

这是我目前所拥有的。我假设您使用 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.')

问题是,当我输入一个数字时,例如数字“4”,程序运行如下:

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

这就是它读给我听的全部内容。那么我该如何解决呢?因为我认为我走在正确的轨道上。

检查使用术语 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 语句链接在一起。这样做比单独评估每个子句更有效。正如您在上面看到的,您可以通过在第一个语句中使用 'if',在后续语句中使用 'elif',在最后一个语句中使用 'else' 来执行此操作。

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

# 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.'

第二个是<>和<=>=的区别。请注意,如果某人未满 1 岁,则作业将其归类为婴儿。这不包括 1 岁,仅包括尚未达到 1 岁的人,因此您将使用“< 1”来获取所有信息至 364 天。

这与将 'at least' 13 岁的人归类为青少年不同。这意味着它们可以是 13 或以上,因此您可以使用“>= 13”。

看来您陷入了一个循环,您需要告诉程序该说什么,例如。 如果年龄 <=1 : 打印 ("you are a infant") 别的: 打印 ("you are a child")

希望这能解决您的问题

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

希望对您有所帮助:

from datetime import date

todayDate = date.today();

DOB = input("Please enter your Date of Birth: ");
currentYear = todayDate.year;

age = (int(currentYear) - int(DOB));
print(f'Your current age is: {age}');

print ("\n")
if age <= 5:
    print("You are a baby.")
elif age >= 5 and age < 13:
    print("You are a toddler")
elif age >= 13 and age < 18:
    print("You are an teenager")
elif age >= 18 and age < 60:
    print("You are an adult.")    
elif age >= 60:
    print("You are a senior Citizen")

谢谢!

Write if-elif-else chain to detrmine a person's stage of life.

    # Ask for input
    age = int(input('Person age: '))
    
    # if the person is less then 2 year old
    if age <= 2:
        print('The person is Baby.')
    # if the person is least 2 years old but less then 4 
    elif age >= 2 and age <= 4:
        print('The person is Infant.')
    # if the person is at least 4 years old but less then 13
    elif age >= 4 and age <= 13:
        print('The person is Kid.') 
    # if the person is at least 13 years old but less then 20
    elif age >= 13 and age <= 20:
        print('The person is Teenager.') 
    # if the person is at least 20 years old but less then 65
    elif age >= 20 and age <= 65:
        print('The person is Adult.') 
    # if the person age 65 or older
    else:
        if age >= 65:
            print('The person is an Elder.')