首页 > 解决方案 > 我的 python 应用程序控制台锁不起作用

问题描述

我正在开发这个 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(
            '{:<15}\t:    {:>}\t     {:<}'.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()

标签: pythoncoding-style

解决方案


推荐阅读