Home > other >  I want to create a function where for every word of the alphabet
I want to create a function where for every word of the alphabet

Time:06-10

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?

CodePudding user response:

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()  

CodePudding user response:

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'
>>>
  • Related