-2

I want to make my python code run a different function as you type, one function assigned per letter.

I would have the code for each letter

def a():
    # The letter A code
def b():
    # The letter B code

Then the input (something like this)

letters = input("Input Text:") 
print (letters)

And when it prints letters it runs the "a" function for a, "b" function for b, and so on. I'm pretty new to python so any help is appreciated.

Simon
  • 83
  • 1
  • 9
  • There is an `if-else` statement. – Lamanus Aug 04 '20 at 14:22
  • 2
    Do you mean the user will enter a string like , say ur name itself "simon" and when user for each letter in the word you will invoke the function? and when print is invoked do u expect simon to be printed on the screen or just the functions to be executed?? – Arun Kaliraja Baskaran Aug 04 '20 at 14:31
  • 1
    @Vasyl Please don't recommend `eval` to beginners. OP hasn't mentioned what context this is going to be used in, so you don't know if you can trust the input. As well, please don't post answers in the comments. – wjandrea Aug 04 '20 at 14:37
  • Does this answer your question? [detect key press in python?](https://stackoverflow.com/questions/24072790/detect-key-press-in-python) – wjandrea Aug 04 '20 at 14:39
  • @ArunKalirajaBaskaran Exactly! Just the functions to be executed... – Simon Aug 04 '20 at 14:40
  • Do you mean that the functions will be called in *real-time*? Like as each key is pressed the corresponding function will be called, or after the input is finished to call the function on each letter of it? – Tomerikoo Aug 04 '20 at 14:54
  • @wjandrea That would be good but I can't figure out how to get it working! – Simon Aug 04 '20 at 14:58

2 Answers2

3

Lets assume that all the functions for each letter is going to be a in a separate module called key_functions.py

In the main program where you are going to process the letters:

import key_functions
def print(letter):
    for letter in letters:
         try:
             key_func = getattr(key_functions, letter)
         except:
             raise
         key_func()


letters = input("Input Text:") 
print(letters)

NOTE: the print function is something which is going to be visible inside this module only!!

  • There's a few problems in this: 1) [A bare `except` is bad practice](https://stackoverflow.com/a/54948581/4518341). Catch the specific exception you're expecting instead, i.e. `AttributeError`. 2) `print` takes a parameter `letter`, but then you iterate over `letters`, which is from the parent scope. [In general you should avoid reusing names from other scopes.](https://stackoverflow.com/q/20125172/4518341) 3) [Avoid shadowing builtins like `print`](https://stackoverflow.com/a/79198/4518341). In your code, you call `print` in the `except` block, which will probably cause a `RecursionError`. – wjandrea Aug 05 '20 at 14:50
  • yeah i agree with the recursion error.. others are kind of okay.. – Arun Kaliraja Baskaran Aug 05 '20 at 16:04
  • @Arun No, the parameter should definitely be fixed. For example, `letters = 0; print('hello')` will error. As well, `except: raise` does practically nothing, but catching `AttributeError` would make sense, and if I were you, I would have it do something like `raise ValueError(f'invalid letter: {letter}')` – wjandrea Aug 05 '20 at 21:00
0
def a():
    print('a ran')

def b():
    print('b ran')

letters = input("Input Text:") 
print (letters)

for l in list(letters): locals()[l]()

The printout if you enter, say 'ababab' with the quote marks would be:

Input Text:'ababab'
ababab
a ran
b ran
a ran
b ran
a ran
b ran
Mike O'Connor
  • 2,494
  • 1
  • 15
  • 17