用python编写一个程序来获取5个用户的居住状态和婚姻状况

用python编写一个程序来获取5个用户的居住状态和婚姻状况,python,Python,我一直在尝试用python编写一个程序,它可以显示5个人的状态,并说明此人居住的地区。我写了我的部分代码,想测试一下西部、东部和南部三个地区中的一个地区的婚姻状况。问题是,即使我把这个州称为“NY”,它仍然是朝着西部而不是朝着东部,我不知道如何解决它。这是我的密码: West = 0 East = 0 South = 0 Married = married = 0 MarriedW = 0 MarriedE = 0 MarriedS = 0 NY = ny = FL = fl = MA = ma

我一直在尝试用python编写一个程序,它可以显示5个人的状态,并说明此人居住的地区。我写了我的部分代码,想测试一下西部、东部和南部三个地区中的一个地区的婚姻状况。问题是,即使我把这个州称为“NY”,它仍然是朝着西部而不是朝着东部,我不知道如何解决它。这是我的密码:

West = 0
East = 0
South = 0
Married = married = 0
MarriedW = 0
MarriedE = 0
MarriedS = 0
NY = ny = FL = fl = MA = ma = East
TX = tx = AL = al = GA = ga = Southern
CA = ca = NV = nv = AR = ar = WA = wa = West
state1 = int(input("please enter the state where the first person resides :"))
status1 = int(input("Please enter your marital status of first person :"))
if (state1==West):
    West = West + 1
    if (status1=='Married' or 'married'):
        MarriedW = MarriedW + 1
elif (state1==East):
    East = East + 1
    if (status1=='Married' or 'married'):
        MarriedE = MarriedE + 1
elif (state1==South):
    South = South + 1
    if (status1=='Married' or 'married'):
        MarriedS = MarriedS + 1
else:
    print("The person is not counted towards Any of the following states. TX, Al, GA, NY, MA, FL, CA, NV, AR or WA")
print("The number of people who belong to Western region is :" +str(West))
print("The number of people who are married in Western region is :" +str(MarriedW))
print("The number of people who belong to the Eastern region is :" +str(East))
print("The number of people who are married in Eastern region is :" +str(MarriedE))
print("The number of people who belong to Southern region is :" +str(South))
print("The number of people who are married in Southern region is:" +str(MarriedS))

问题是,您将状态指定为值,这些值等于已定义的值(东-西-南),这些值设置为0。换言之,您正在使所有内容都等于0(因此West的第一个条件检查总是会被命中)

代码很混乱,但要使其在最小的更改下工作,请将状态变量设置为字符串值,并对照条件中的字符串值进行检查,如下所示:

West = 0
East = 0
South = 0
Married = married = 0
MarriedW = 0
MarriedE = 0
MarriedS = 0
NY = ny = FL = fl = MA = ma = 'East' #changed this to a string
TX = tx = AL = al = GA = ga = 'South' #changed this to a string
CA = ca = NV = nv = AR = ar = WA = wa = 'West' #changed this to a string
state1 = input("please enter the state where the first person resides :") #removed int cast
print(state1)
status1 = input("Please enter your marital status of first person :")
if (state1=='West'): #changed to check against string
    West = West + 1
    if (status1=='Married' or 'married'):
        MarriedW = MarriedW + 1
elif (state1=='East'):#changed to check against string
    East = East + 1
    if (status1=='Married' or 'married'):
        MarriedE = MarriedE + 1
elif (state1=='South'): #changed to check against string
    South = South + 1
    if (status1=='Married' or 'married'):
        MarriedS = MarriedS + 1
else:
    print("The person is not counted towards Any of the following states. TX, Al, GA, NY, MA, FL, CA, NV, AR or WA")
print("The number of people who belong to Western region is :" +str(West))
print("The number of people who are married in Western region is :" +str(MarriedW))
print("The number of people who belong to the Eastern region is :" +str(East))
print("The number of people who are married in Eastern region is :" +str(MarriedE))
print("The number of people who belong to Southern region is :" +str(South))
print("The number of people who are married in Southern region is:" +str(MarriedS))
而不是使用:

 if (status1=='Married' or 'married')
使用:

您还可以删除多余的文件:

 if status1 == 'Married' or status1 == 'married'
更好地使用:

if status1.lower() == 'married'

如果您想检查用户的输入(在您的案例中为状态代码)是否符合上述任何状态,我认为解决此问题的实用且稍微优雅的解决方案是为状态创建字符串列表,例如:

East_states=['NY','NY','FL','FL','MA','MA']

然后检查列表中是否可以找到用户输入(在Python3中默认为字符串):

    state1 = input("please enter the state where the first person resides :")
    status1 = input("Please enter your marital status of first person :")
if (state1 in West_states):
    West = West + 1
    if (status1 == 'married'):
        MarriedW = MarriedW + 1'

这是一个相当湿和野生的编码,我建议将变量构造成某种容器,如字典或列表,以使其更易于管理无论
NY=NY=FL=FL=MA=MA=East
是什么,都应该是其他的。此代码不作为isOr运行,为了进一步简化,为什么不
if status1.lower()=='marred'
并去掉第二个条件altogether或者
if status1 in{'marred',marred'}:
。这是进一步帮助OP编写代码的一个好方法,但它没有回答OP最初提出的问题-这就是为什么将纽约地图传递到西部而不是东部。
    state1 = input("please enter the state where the first person resides :")
    status1 = input("Please enter your marital status of first person :")
if (state1 in West_states):
    West = West + 1
    if (status1 == 'married'):
        MarriedW = MarriedW + 1'