Моя блокировка консоли приложения python не работает

#python #coding-style

Вопрос:

Я работаю над этим приложением на python. Я хочу, чтобы все остальные кнопки блокировались, когда участник входит. Я создал переменную lock = False , но она, похоже, не работает. Я думаю, что это проблема с отступом, но я точно не уверен и нуждаюсь в некоторой помощи.

 import pps_emu import os   def main():  sensor = pps_emu.Sensor() # Establish connection with pps_emu sensor.  clear = lambda: os.system('cls')   contest_controls = [ # Set up explanation of the controls and the orientation to the contestants and host  ['Contestant 1', 'Button_1', 'rled'],  ['Contestant 2', 'Button_2', 'yled'],  ['Contestant 3', 'Button_3', 'gled'],  ['Game Show Host', 'Button_4', 'None']]   # Print statements for the game show instructions  print('********************************************')  print('Welcome to the Quiz Game Buzzer Application')  print('********************************************')  print()  print('This program allows contestants to buzz in to answer questions for the')  print('Are you smarter than Mr. Sciallo game. It will print status messages to the screen.')  print('We will use the following button and light assignment.')  print()  print('Contestant/Host: t Button: t LED:')  print('********************************************')   for i in contest_controls:  print(  '{:lt;15}t: {:gt;}t {:lt;}'.format(i[0], i[1], i[2])) # Prints contestant controls with the instructions   print()  print('Press buzzer to answer question')  lock = False  while True: # start the forever loop  contestant = 0 # initialize the contestant variable  if sensor.input('Button_1') == 'pressed': # Contestant 1 button set up  if lock:  print('Another contestant is already buzzed in')  else:  lock = True  sensor.output('rled', 'on')  sensor.output('buzz', 'on')  print('Contestant 1 is buzzed in!')   if sensor.input('Button_2') == 'pressed': # Contestant 2 button set up  if lock:  print('Another contestant is already buzzed in')  else:  lock = True  sensor.output('yled', 'on')  sensor.output('buzz', 'on')  print("Contestant 2 buzzed in!")   if sensor.input('Button_3') == 'pressed': # Contestant 3 button set up  if lock:  print('Another contestant is already buzzed in')  else:  lock = True  sensor.output('gled', 'on')  sensor.output('buzz', 'on')  print("Contestant 3 buzzed in!")   if sensor.input('Button_4') == 'pressed': # Host button set up  sensor.output('rled', 'off')  sensor.output('yled', 'off')  sensor.output('gled', 'off')   if sensor.input('light') == 'on': # Quit if user turn on the light sensor  print("Quitting") # print quitting  clear()  quit()   break # break the loop  host = 0 # initialize the host variable  while host == "Button_4": # start other loop  if sensor.input('Button_4') == 'pressed': # Host button set up  print("Host Pressed") # print host pressed  sensor.output('rled', 'off')  sensor.output('yled', 'off')  sensor.output('gled', 'off')   if __name__ == "__main__": # Tell the Python interpreter to execute this main()  main()  

Комментарии:

1.Попробуй, внутри while True: петли, сделать это if elif elif else . Вместо ваших текущих нескольких if ов