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



我是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)

你几乎已经完成了。但是在if条件中有一个小错误。您需要的条件应如下所示:

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

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

最新更新