我是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)
答案 0 :(得分:2)
if (chk == 'y' and sw1 == '1'):
这意味着你应该在单引号'1'中保留1,因为它是一个字符串