Reputation: 1
I'm trying to learn Python to do some projects in the raspberry Pi, so I am still very new to all things coding. I'd like to apply a function to every letter in a string, including spaces and repeated letters. I had first converted it to a list, and then changed it to use the tip from one of the answers here, but so far it seems like for two equal elements of the same list the function is only applied to one of them. In this case for example, only one L and one O are present on the output, and no space (space should output a 0.7s pause). Here is the exact code of the very simple program I am trying to write, based on one of monkmakes.com projects. Basically the idea is to traslate a string into morse code by lighting a LED connected to a Raspberry Pi. So far it is meant to only handle the message "Hello World":
import RPi.GPIO as GPIO
import time
import itertools
#define basic blink styles for morse
#length of a dot is one unit
#length of a dash is three units
def fast_blink():
# Configure the Pi to use the BCM (Broadcom) pin names, rather than the pin positions
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
GPIO.output(red_pin, True) # LED on
time.sleep(0.1) # delay 0.1 seconds
GPIO.output(red_pin, False) # LED off
time.sleep(0.1) # delay 0.1 seconds
def slow_blink():
# Configure the Pi to use the BCM (Broadcom) pin names, rather than the pin positions
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
GPIO.output(red_pin, True) # LED on
time.sleep(0.3) # delay 0.1 seconds
GPIO.output(red_pin, False) # LED off
time.sleep(0.1) # delay 0.1 seconds
#Morse Code Letter Blink Funcions
#Define fast_blink and slow_blink repetitions
#The space between parts of the same letter is one unit
#The space between letters is three units
#define H code
def morse_H():
print("H")
for _ in itertools.repeat(None, 4):
fast_blink()
time.sleep(0.3)
#define E code
def morse_E():
print("E")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.3)
#define L code
def morse_L():
print("L")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
fast_blink()
time.sleep(0.3)
#define O code
def morse_O():
print("O")
for _ in itertools.repeat(None, 3):
slow_blink()
time.sleep(0.3)
#define W code
def morse_W():
print("W")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
slow_blink()
time.sleep(0.3)
#define R code
def morse_R():
print("R")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.3)
#define D code
def morse_D():
print("D")
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
fast_blink()
time.sleep(0.3)
#MESSAGE INPUT
message = input("Type your message \n")
print("Translating {} to morse, look at the LED" .format(message))
len_message = len(message)
print("Message is {} characters long." .format(len_message))
#MESSAGE TRANSLATION
#Create translation function
def m_translate(char):
if "H" or "h":
morse_H()
else:
pass
if "E" or "e":
morse_E()
else:
pass
if "L" or "l":
morse_L()
else:
pass
if "O" or "o":
morse_O()
else:
pass
if "W" or "w":
morse_W()
else:
pass
if "R" or "r":
morse_R()
else:
pass
if "D" or "d":
morse_D()
else:
pass
if " ":
print("\n")
time.sleep(0.7)
else:
pass
#MESSAGE OUTPUT
#Execute in endless loop
try:
while True:
for _ in itertools.repeat(None):
message_list = list(map(m_translate, message))
print ("\n" * 100)
except KeyboardInterrupt:
print("Transmission ended.")
GPIO.cleanup()
quit
Could anyone tell me how to apply a function to every string (character)in the list, even repeated elements and spaces? Thank you in advance!
Upvotes: 0
Views: 1294
Reputation: 1
I had help from a friend who tweaked the code, rearranging the method conditions to Elif and replacing "char" as the iterative by something else. Apparently "char" was causing issues. The final loop was also cleaned up. Here is the final working code:
import RPi.GPIO as GPIO
import time
import itertools
#define basic blink styles for morse
#length of a dot is one unit
#length of a dash is three units
def fast_blink():
# Configure the Pi to use the BCM (Broadcom) pin names, rather than the pin positions
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
GPIO.output(red_pin, True) # LED on
time.sleep(0.1) # delay 0.1 seconds
GPIO.output(red_pin, False) # LED off
time.sleep(0.1) # delay 0.1 seconds
def slow_blink():
# Configure the Pi to use the BCM (Broadcom) pin names, rather than the pin positions
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
GPIO.output(red_pin, True) # LED on
time.sleep(0.3) # delay 0.1 seconds
GPIO.output(red_pin, False) # LED off
time.sleep(0.1) # delay 0.1 seconds
#Morse Code Letter Blink Funcions
#Define fast_blink and slow_blink repetitions
#The space between parts of the same letter is one unit
#The space between letters is three units
#define H code
def morse_H():
print("H")
for _ in itertools.repeat(None, 4):
fast_blink()
time.sleep(0.3)
#define E code
def morse_E():
print("E")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.3)
#define L code
def morse_L():
print("L")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
fast_blink()
time.sleep(0.3)
#define O code
def morse_O():
print("O")
for _ in itertools.repeat(None, 3):
slow_blink()
time.sleep(0.3)
#define W code
def morse_W():
print("W")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
slow_blink()
time.sleep(0.3)
#define R code
def morse_R():
print("R")
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 1):
fast_blink()
time.sleep(0.3)
#define D code
def morse_D():
print("D")
for _ in itertools.repeat(None, 1):
slow_blink()
time.sleep(0.1)
for _ in itertools.repeat(None, 2):
fast_blink()
time.sleep(0.3)
#MESSAGE INPUT
message = input("Type your message \n")
message = message.upper()
print("Translating {} to morse, look at the LED" .format(message))
len_message = len(message)
print("Message is {} characters long." .format(len_message))
#MESSAGE TRANSLATION
#Create translation function
def m_translate(letter):
if letter == "H":
morse_H()
elif letter == "E":
morse_E()
elif letter == "L":
morse_L()
elif letter == "O":
morse_O()
elif letter == "W":
morse_W()
elif letter == "R":
morse_R()
elif letter == "D":
morse_D()
elif letter == " ":
print("\n")
time.sleep(0.7)
else:
pass
#MESSAGE OUTPUT
#Execute in endless loop
try:
while True:
for letter in message:
m_translate(letter)
print ("\n" * 100)
except KeyboardInterrupt:
print("Transmission ended.")
GPIO.cleanup()
quit
Upvotes: 0
Reputation: 396
you can use the map function:
list_name = list(map(function_name, iterable_variable))
where 'list_name' is the variable that holds the results, 'function_name' is the function you wish to apply to all elements in a list and 'iterable_variable' is any variable that can be looped through, such as a list or a string. you can even skip turning the string into a list as this does that too.
Upvotes: 1