Posts: 8
Joined: Mon Jan 16, 2017 12:49 pm

List / array help request

Mon Oct 23, 2017 9:34 am

Hi everyone,

I am trying to create a python program to do the following:

- accept a list of instruction inputs and length of time to execute the instructions from the user
- read the instructions from the list, assign its time value and execute in a first in first out structure
- all instructions must be on the list before any instructions are executed

The goal of the program is to use the Explorer Hat Pro and its LEDs and capacitive buttons to input the instruction (activate an LED) and a keyboard to add a time value, use the return / enter key on the keyboard to end data input and start the execution of instructions. As an example the user might press button 1 on the explorer hat, enter 5 on a keyboard then button 2 on the hat followed by 2 on the keyboard. When enter is selected an LED with name '1' is turned on for 5 seconds followed by led '2' for 2 seconds. Below is an example of what i want to achieve in pseudo code:

Code: Select all

#This is pseudo code

# create to lists or arrays to store data
define instruction list / array
define time list / array

#instruction list
up_arrow = led1.on
down_arrow = led2.on
right_arrow = led3.on
left_arrow = led4.on
x = execute commands
p = pulse all leds

#get user input and add to relevant list / array
print - "please enter instruction: "
get user input # via keyboard
add input to instruction list / array
print - "how long will this instruction last: "
get user input # via keyboard
add time value to time list / array

#execute commands
count items in both lists #both lists should have the same number of values
if instruction_list_count = time_list_count: #error checking
	l = total values in list
	while true:
		For i in range(l):
			get first value from instruction list
			get first value from time list
			execute instruction for specified time
			remove executed instruction and time from lists
			repeat loop until all instructions have been completed

The part I am having difficulty with is getting the data entry into a list or array with a time value then recovering the instruction and executing it with a given time value. I hope the pseudo code above helps explain what I am trying to do. Any questions please ask and thank you in advance to anyone who can help.

User avatar
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: List / array help request

Mon Oct 23, 2017 12:02 pm

I would create a 2d list. It will be a list of lists. The lowset list will contain both the command and the time taken and the upper list will be made up of the lower lists

See my code and you will need just to add to it to create what you want.

Code: Select all

#create a empty list of commands
command_list = []

#get user input for command and time for running the commond
user_command = keyboard input  #replace keyboard input with your code for getting user input
user_time = keyboard input 
command_list.append([user_command, user_time]) # append the list with a list of both the command and time
#repeat as many times as you need to populate theb list

#if you want to know the length of the list
length_of_list = len(command_list)

#read and remove each command and time from the list till all items r removed
while len(command_list)>0:
   command_to_do, time_to_do = command_list.pop(0) #read the command and time from first position and remove it from the list
   #now add code to execute command_to_do for time_to_do

Posts: 8
Joined: Mon Jan 16, 2017 12:49 pm

Re: List / array help request

Mon Oct 23, 2017 12:41 pm

Thanks for the swift reply. I will try this out later in the week when I can spend some quality time with my Pi.

Posts: 8
Joined: Mon Jan 16, 2017 12:49 pm

Re: List / array help request

Sun Nov 12, 2017 9:37 pm

OutoftheBOTS - Thank you for your help, that code worked really well once I'd got my head around everything else that I wanted to do. Thank you once again.

Return to “Python”