python中的局部和全局变量概念

时间:2017-11-03 06:26:08

标签: python python-3.x

我是python编程的新手。我对局部和全局变量概念有一些实现问题。我已经阅读了有关全局和局部变量的一些答案。我尝试使用这个概念,但是,在下面的代码中,我没有得到正确的输出。当我按下' y'它显示了0号房间的人数。

我的代码:

sw1 = 0
sw2 = 0
d1sen1 = 1
d1sen2 = 0
d2sen1 = 1
d2sen2 = 0
chk = 0 
d = 0
count = 0
def dgopenig_operation(x):
    d1sen1 = 0
    d1sen2 = 0
    d2sen1 = 0
    d2sen2 = 0
    print ("the door is going to open")
def doppened_operation():
    d1sen1 = 0
    d1sen2 = 1
    d2sen1 = 0
    d2sen2 = 1
    print ("the door is oppened u can pass now")    
def dgclose_operation(x):
    d1sen1 = 0
    d1sen2 = 0
    d2sen1 = 0
    d2sen2 = 0
    print ("the door  is  going to closed")   
def close_operation():
    d1sen1 = 0
    d1sen2 = 0
    d2sen1 = 0
    d2sen2 = 0
    print ("the door is going to closed") 
sw1 = input("enter 1 for enteringing the room")
dgopenig_operation(sw1)
doppened_operation()
chk = input("are u passed or not? y/n")
if (chk == 'y' and sw1 == 1):
    dgclose_operation(1)
    global count
    count = count +1    
else:
    print ('the door is still oppening')

print( 'the no of person  in the room is ',count)

1 个答案:

答案 0 :(得分:2)

你几乎完成了。但如果情况有一个小错误。您的所需条件应如下所示:

if (chk == 'y' and sw1 == '1'):

这意味着你应该在单引号'1'中保留1,因为它是一个字符串