Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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 输出为';这不是我所期望的_Python - Fatal编程技术网

Python 输出为';这不是我所期望的

Python 输出为';这不是我所期望的,python,Python,我正在使用while循环编写一个程序。我想写一个程序来询问3个人的婚姻状况和年龄。我遇到的问题是,如果我输入了“已婚”以外的输入,它不会将其添加到正确的输出中。所以我要问的是,我如何使它成为这样,当我输入例如“single”时,输出会说,单身人数是:1? 这是我的密码: marriedstud=0 sepstud=0 marriedover50=0 married40s=0 married30s = 0 married20s = 0 sepover50=0 sep40s=0 sep30s = 0

我正在使用while循环编写一个程序。我想写一个程序来询问3个人的婚姻状况和年龄。我遇到的问题是,如果我输入了“已婚”以外的输入,它不会将其添加到正确的输出中。所以我要问的是,我如何使它成为这样,当我输入例如“single”时,输出会说,
单身人数是: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("*****")
marriedstud=0
sepstrud=0
结婚50=0
结婚40秒=0
结婚30秒=0
结婚20秒=0
sepover50=0
sep40s=0
sep30s=0
sep20s=0
离婚螺柱=0
离婚超过50=0
离婚40s=0
离婚30秒=0
离婚20秒=0
单程50=0
single40s=0
单程30秒=0
单程20秒=0
单螺柱=0
年龄=0
计数=0
而(计数<2):
maritalstatus=输入(“请输入人员婚姻状况:”)
年龄=输入(“请输入佩隆年龄:”)
如果(maritalstatus==“已婚”)或(maritalstatus==“已婚”):#我尝试使用'and'和'or',但都不起作用
marriedstud=marriedstud+1
如果(年龄>=50):
marriedover50=marriedover50+1
elif(年龄>=40)和(年龄=30)以及(年龄=20)和(年龄=50):
singleover50=singleover50+1
elif(年龄>=40)和(年龄=30)以及(年龄=20)和(年龄=50):
离婚超过50=离婚超过50+1
elif(年龄>=40)和(年龄=30)以及(年龄=20)和(年龄=50):
sepover50=sepover50+1

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

int(age)
而不仅仅是:

age

如果这有帮助,请标记为正确。如果
age=50
,或者
age=40
。@deceze这不是为什么我运行它时,程序没有将其计入正确状态的原因,除了结婚这是真的。但这是你的逻辑中一个巨大的明显缺陷,需要指出。另一个大问题是,你只是检查
maritalstatus==“单身”
如果
age
与您的任何检查不匹配。如果您的逻辑工作正常(请参阅第一条注释),只有<20岁的人才会被考虑单身、离婚或分居。@deceze要么我不明白你在说什么,要么你错了。当我运行这个程序时,我输入的所有年龄都被归入正确的年龄组,但它只属于
已婚
状态。这也是一个问题,但不是唯一的问题。哦,我看了在您的个人资料中,我现在将避开您的方式,让您诚实地回答问题。这类问题没有什么有趣的问题,但需要花费大量精力才能正确回答。我很高兴在评论中给出提示,并希望其他人写出所需的正确答案。;)好的。另外,你介意看看我几个小时前问的一个问题吗?我想你可以通过我的个人资料找到它