[Python] How to iterate inputs depending on contents of separate list

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have 2 functions. My goal is to ask the user to connect some cables which are pre defined (according to the specific test they are running), confirm they have done this, then ask the user to confirm which COMPorts they wish to use.

    My issue (located in the define_ports() function) is is that I would like to know how to dynamically spit out inputs for the user, depending on how many cable connections I have previously defined. Apologies if this is simple, it's been a long day and my brain isn't working right now.

    Code below:

    ports = list(serial.tools.list_ports.comports())
    conec1 = 'USB Captive Cable'
    conec2 = 'Serial RS232 Cable'
    conec3 = None
    cons = [conec1, conec2, conec3]
    spar = 0
    conec_list = []
    for i in cons:
    if i != None:
    conec_list.append(i)
    spar += 1

    def Setup():
    def test_setup(conec1, conec2, conec3):
    "Details what cables need to be connected before test can progress"
    print('\n> > For this test you will need to connect the following:\n')
    for i in conec_list:
    if i != None:
    print(i)
    while True:
    yes = input('> > Please confirm this has been done. y/n ')
    if yes != 'y':
    continue
    else:
    break
    return conec1, conec2, conec3

    def define_ports():
    "Asks user to confrim which Port numbers are to be used for each port connection"
    test_setup(conec1, conec2, conec3)
    print('\n> > The available Port connections are: ')
    for p in ports:
    print(p)
    while True:
    print('\n> > Please confirm which Port numbers are to be used for each connection\n')
    global prt1
    global prt2
    global prt3

    for i in conec_list:
    if i != None:
    prt1 = input(f'- Port number for {conec1}: ')
    prt2 = input(f'- Port number for {conec2}: ')
    prt3 = input(f'- Port number for {conec3}: ')

    print('\n> > The Port numbers you have selected are: '
    f'{conec1} = {prt1}, '
    f'{conec2} = {prt2}'
    f'{conec3} = {prt3}')

    flash = input('Is this correct? y/n ')
    if flash != 'y':
    continue
    else:
    break
    prt1 = str('COM'+prt1)
    prt2 = str('COM'+prt2)
    prt3 = str('COM'+prt3)

    define_ports()

    Setup()


    The issue i'm running into at the minute is getting the inputs (prt1, prt2, prt3) to be presented to the user depending on how many items there are in conec_list. I have tried various for loops but am not sure if this is the best way to try.

    Continue reading...

Compartilhe esta Página