输出不是我期望的

2024-04-24 07:44:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用while循环编写一个程序。我想写一个程序询问3个人的婚姻状况和年龄。我遇到的问题是,如果我输入的不是“married”,它就不能将它添加到正确的输出中。所以我要问的是,当我输入一个,例如“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("*****")

Tags: andoftheinnumberageisgroup
1条回答
网友
1楼 · 发布于 2024-04-24 07:44:31

这是数据类型之间的冲突。当用户输入婚姻状况之后的年龄时,他们会将其作为字符串输入,您无法将其与20、30、40等整数进行比较。您需要执行以下操作:

int(age)

而不仅仅是:

age

如果这有帮助,请标记为正确

相关问题 更多 >