-2

I want to create a function where for every word of the alphabet the user uses for an input, the console returns the following: a = 0 b = 00 c - 000 And sow on... Example: Sow if the user put the input of "abc", the console will print out: 000000 In my code, i can't seem to add the letters, hers the code:

def codifier():  
    userImp = input(str("write a word: "))   
    if userImp == "a": 
        print("0") 
    else:
        userImp == "b"
        print("00")
        
    print(userImp)

codifier()   

MY question is how would you write the code?
  • 1
    [Are you just looking for `elif`?](https://docs.python.org/3/tutorial/controlflow.html) – David Jun 09 '22 at 17:44

2 Answers2

0

Here is a simple program that does what you are asking for:

def codifier():  
    userImp = input(str("write a word: "))   
    for char in userImp:
        print(end="0" * (ord(char) - ord('a') + 1))

codifier()  
Blackgaurd
  • 608
  • 6
  • 15
0

Make a dictionary mapping characters to the symbol you want.

>>> m = {'a':'0','b':'00', 'c':'000'}

Then use it along with a user's input

>>> r = input('??')
??ab
>>> ''.join(m[c] for c in r)
'000'
>>> r = input('??')
??ac
>>> ''.join(m[c] for c in r)
'0000'
>>> r = input('??')
??abc
>>> ''.join(m[c] for c in r)
'000000'
>>>
wwii
  • 23,232
  • 7
  • 37
  • 77
  • While using a dictionary to map the number of 0's is nice, OP says they want to do this for every letter. So doing creating it manually with 26 items is not ideal. Maybe add a way to generate that dictionary before using its values for the join. – aneroid Jun 09 '22 at 18:25
  • Sure, there is a way to do that but that's not what I was answering. – wwii Jun 09 '22 at 18:30
  • Do uppercase come after lowercase?? Or is the sequence `'aAbBcC...'`? for automating the dict construction? – wwii Jun 09 '22 at 18:35
  • Fair enough. OP was unclear. I would have assumed only lowercase. Or "either case" mapping to the same output - by using `m[c.lower()]` in the join expression. – aneroid Jun 10 '22 at 05:48