python - 输出不是我预期的
问题描述
我正在使用 while 循环编写程序。我正在尝试编写一个程序来询问 3 人的婚姻状况和年龄。我遇到的问题是,如果我输入“已婚”以外的输入,它不会将其添加到正确的输出中。所以我要问的是如何做到这一点,以便当我输入例如“single”时,输出会显示,the number of people who are single is: 1
?这是我的代码:
marriedstud=0
sepstud=0
marriedover50=0
married40s=0
married30s = 0
married20s = 0
sepover50=0
sep40s=0
sep30s = 0
sep20s = 0
divorcedstud=0
divorcedover50=0
divorced40s = 0
divorced30s = 0
divorced20s = 0
singleover50=0
single40s=0
single30s=0
single20s = 0
singlestud=0
age=0
count = 0
while (count < 2):
maritalstatus= input("Please enter persons marital status:")
age =input("Please enter perons age: ")
if (maritalstatus=="MARRIED") or (maritalstatus=="married"): #I tried using 'and' and 'or' but neither work
marriedstud = marriedstud + 1
if (age>=50):
marriedover50 = marriedover50 + 1
elif (age>=40) and (age<50):
married40s = married40s + 1
elif (age>=30) and (age<40):
married30s = married30s + 1
elif (age>=20) and (age<30):
married20s = married20s + 1
else:
if (maritalstatus=="SINGLE") or (maritalstatus=="single"):
singlestud = singlestud + 1
if (age>=50):
singleover50 = singleover50 + 1
elif (age>=40) and (age<50):
single40s = single40s + 1
elif (age>=30) and (age<40):
single30s = single30s + 1
elif (age>=20) and (age<30):
single20s = single20s + 1
else:
if (maritalstatus=="DIVORCED") or (maritalstatus=="divorced"):
divorcedstud = divorcedstud + 1
if (age>=50):
divorcedover50 = divorcedover50 + 1
elif (age>=40) and (age<50):
divorced40s = divorced40s + 1
elif (age>=30) and (age<40):
divorced30s = divorced30s + 1
elif (age>=20) and (age<30):
divorced20s = divorced20s + 1
else:
if(maritalstatus=="SEPARATED") or (maritalstatus=="separated"):
sepstud = sepstud + 1
if (age>=50):
sepover50 = sepover50 + 1
elif (age>=40) and (age<50):
sep40s = sep40s + 1
elif (age>=30) and (age<40):
sep30s = sep30s + 1
elif (age>=20) and (age<30):
sep20s = sep20s + 1
count += 1
print("The number of pepole who are married is: " +str(marriedstud))
print("The number of pepole who are married and over the 50 is: " +str(marriedover50))
print("The number of pepole who are married and in the age group of 40's is: " +str(married40s))
print("The number of pepole who are married and in the age group of 30's is: " +str(married30s))
print("The number of pepole who are married and in the age group of 20's is: " +str(married20s))
print("*****")
print("The number of pepole who are single is: " +str(singlestud))
print("The number of pepole who are single and over the 50 is: " + str(singleover50))
print("The number of pepole who are single and in the age group of 40's is: " +str(single40s))
print("The number of pepole who are single and in the age group of 30's is: " +str(single30s))
print("The number of pepole who are single and in the age group of 20's is: " +str(single20s))
print("*****")
print("The number of pepole who are divorced is: " +str(divorcedstud))
print("The number of pepole who are divorced and over the 50 is: " +str(divorcedover50))
print("The number of pepole who are divorced and in the age group of 40's is: " +str(divorced40s))
print("The number of pepole who are divorced and in the age group of 30's is: " +str(divorced30s))
print("The number of pepole who are divorced and in the age group of 20's is: " +str(divorced20s))
print("*****")
print("The number of pepole who are separated is: " +str(sepstud))
print("The number of pepole who are separated and over the 50 is: " +str(sepover50))
print("The number of pepole who are separated and in the age group of 40's is: " +str(sep40s))
print("The number of pepole who are separated and in the age group of 30's is: " +str(sep30s))
print("The number of pepole who are separated and in the age group of 20's is: " +str(sep20s))
print("*****")
解决方案
这是数据类型之间的冲突。当用户输入结婚后的年龄时,他们将其作为字符串输入,您无法与 20、30、40 等整数进行比较。您需要这样做:
int(age)
而不仅仅是:
age
如果这有帮助,请标记为正确
推荐阅读
- node.js - 我不确定为什么在使用 Async.forEachLimit 时会出现 undefined
- node.js - 创建集合时,文档会立即添加到其中
- r - 宽到长、不一致的列名
- python-3.x - BS4 搜索和替换'src' 和 'style' 属性
- postgresql - AWS 数据管道 - RDS Postgres 到 Redshift
- c - 在 C 中使用链表的基本队列实现导致意外输出
- postgresql - 错误连接到 PostgreSQL 无法腌制 psycopg2.extensions.connection 对象
- typescript - TypeScript 中的一些语法糖,用于值与类型偶数的情况?
- css - 在 css 中仅选择顶级 UL
- powershell - 如何使用 PowerShell 按名称过滤具有不同扩展名文件的文件?