Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我处理大于99的数字时,我的代码开始产生不合逻辑的答案_Python - Fatal编程技术网

Python 当我处理大于99的数字时,我的代码开始产生不合逻辑的答案

Python 当我处理大于99的数字时,我的代码开始产生不合逻辑的答案,python,Python,我试图写一个程序来决定一个人的人生阶段,无论这个人是婴儿、儿童、青少年、成年人还是老年人。程序从一个变量开始,该变量包含一个输入函数,该函数允许用户键入年龄,以便程序可以开始运行 问题是,当我输入的年龄大于99岁时,程序会提示输入年龄的人是婴儿。所以基本上,根据该计划,123岁的人是婴儿,这是没有意义的 age=input("enter the person's age: ") if age<'2': print('the person is a baby') elif age

我试图写一个程序来决定一个人的人生阶段,无论这个人是婴儿、儿童、青少年、成年人还是老年人。程序从一个变量开始,该变量包含一个输入函数,该函数允许用户键入年龄,以便程序可以开始运行

问题是,当我输入的年龄大于99岁时,程序会提示输入年龄的人是婴儿。所以基本上,根据该计划,123岁的人是婴儿,这是没有意义的

age=input("enter the person's age: ")

if age<'2':
    print('the person is a baby')

elif age=='2' and age<'4':
    print('the person is a toddler')

elif age >='4' and age<'13':
    print ('the person is a kid')

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

elif age>='20' and age<'65':
    print('the person is an adult')

elif age>='65':
    print('the person is an elder')

我想知道我是否在代码上犯了错误,尽管在我看来这很直截了当。无论如何,我猜我缺少一些理论知识,如果是这样的话,我希望你们能对整个问题有所了解。

你们在这里所做的基本上是比较字符串,而不是转换为你们所寻求的行为。首先需要将输入转换为int

如果您知道输入保证格式正确,请使用:

年龄=输入此人的年龄: 但是,没有人是完美的,所以最好尝试一下,除了:

年龄=输入此人的年龄: 尝试: 年龄=摄入量 除: 处理异常 如果年龄<2岁: 做点什么 埃利夫。。。 多做事
实际上,您在这里所做的是比较字符串,而这些字符串不会转换为您正在寻找的行为。首先需要将输入转换为int

如果您知道输入保证格式正确,请使用:

年龄=输入此人的年龄: 但是,没有人是完美的,所以最好尝试一下,除了:

年龄=输入此人的年龄: 尝试: 年龄=摄入量 除: 处理异常 如果年龄<2岁: 做点什么 埃利夫。。。 多做事
您比较的字符串没有整数,因此“100”小于“99”,因为第一个和第二个字符小于。
您需要将输入转换为整数,然后使用该整数进行比较,然后就可以了

您比较的字符串没有整数,因此“100”小于“99”,因为第一个和第二个字符小于。 您需要将输入转换为整数,然后使用该整数进行比较,然后就可以了

您应该将年龄转换为整数,然后进行比较。你在比较字符串,这会给你奇怪的答案

重新格式化的代码:

age_str=input("enter the person's age: ")

try:
    age = int(age_str)
except:
    print("Invalid Entry. Enter age as a number.")
    exit(0)

if age < 2:
    print('the person is a baby')
elif age == 2 and age < 4:
    print('the person is a toddler')
elif age >= 4 and age < 13:
    print('the person is a kid')
elif age >= 13 and age < 20:
    print('the person is a teenage r')
elif age >= 20 and age < 65:
    print('the person is an adult')
elif age >= 65:
    print('the person is an elder')
代码还检查上面的无效条目。如果输入的不是整数,则会抛出错误。

您应该将年龄转换为整数,然后进行比较。你在比较字符串,这会给你奇怪的答案

重新格式化的代码:

age_str=input("enter the person's age: ")

try:
    age = int(age_str)
except:
    print("Invalid Entry. Enter age as a number.")
    exit(0)

if age < 2:
    print('the person is a baby')
elif age == 2 and age < 4:
    print('the person is a toddler')
elif age >= 4 and age < 13:
    print('the person is a kid')
elif age >= 13 and age < 20:
    print('the person is a teenage r')
elif age >= 20 and age < 65:
    print('the person is an adult')
elif age >= 65:
    print('the person is an elder')

代码还检查上面的无效条目。如果输入的不是整数,它将抛出一个错误。

因为您是在字符串上操作,而不是在数字上操作,所以它会检查字符串是否小于,并且任何“1xx”都将排序为小于任何“2xxx”的内容,更改为intinput。。。然后处理数字,例如,如果年龄<2:所有其他条件也一样,因为您是在处理字符串,而不是数字,所以它会检查字符串是否较小,并且任何“1xx”将排序为小于任何“2xxx”的内容,更改为InInput。。。然后处理数字,例如,如果年龄<2岁:所有其他情况也是如此