我正在使用while循环编写程序。我正在尝试编写一个程序来询问3人的婚姻状况和年龄。我遇到的问题是,如果我输入的内容不是“已婚”,则不会将其添加到正确的输出中。因此,我要问的是如何做到这一点,以便在输入“单一”输入时,输出显示为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("*****")
答案 0 :(得分:0)
这是数据类型之间的冲突。当用户输入结婚年龄后的年龄时,他们将其作为字符串输入,您无法将其与20、30、40等整数进行比较。您需要执行以下操作:
int(age)
而不只是:
age
如果有帮助,请标记为正确