Where I am doing wrong

0 votes
  1. A simple substitution code codes 'a' as 1, 'b' as 2, ..., 'z' as 26. Write a function called "encode" that takes in a string, and prints the coded text. The output should be a string, with letters separated by ".". Use "999" for punctuation and spaces. For example, encode("Hello!") should print out "8.5.12.12.15.999".

Ans)

import string
def encode(letter):
    letter=a,b=1,2
    Nums = range(len(letter))
    New_num = []
    for x in Nums:
         New_num.append(x+1)
         mapping = dict(zip(letter, New_num))
    for letter in letterlist:
        if letter in mapping:
            print(mapping[letter],".",sep="",end="")
        else:
            print("999",".",sep="",end="")
    
encode("Hello!")

Sep 5, 2020 in Python by Sana
• 120 points
1,279 views

1 answer to this question.

0 votes

Hello @Sana,

Lines 3, 6-8 look very sketchy.Here is the solution how to do that:

def encode(s):
    a,z = ord("a"), ord("z")
    return ".".join(
        str(ord(c) - a + 1)
        if ord(c) in range(a, z + 1) else "999"
        for c in s.lower()
    )
print(encode("Hello!"))

To be more simple You can try this also:

def encode(strings):
    import string
   
    s=''
    d={}
    for i in string.ascii_lowercase:
        d[i]=str(ord(i)-96)
    d[' ']='999'
    d['!']='999'
    s=''
    for i in strings.lower():
        s+=d.get(i,i)+'.'
    return s[0:-1]
    
print(encode("Hello!"))
answered Sep 7, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

Where can I get the list of Python keywords?

Just import a module “keyword”. Here you ...READ MORE

answered Apr 20, 2018 in Python by aayushi
• 750 points
685 views
0 votes
1 answer

Why am I getting a error when I am printing two different data types in python?

different data type is being used. that ...READ MORE

answered Mar 6, 2019 in Python by Mohammad
• 3,230 points
1,044 views
0 votes
0 answers

where and how can i use PYTHONPATH in python?

is there a particular syntax for this?? READ MORE

Apr 8, 2019 in Python by Waseem
• 4,540 points
620 views
0 votes
1 answer

Where do I get the Alpha test version of Python?

Hi @Jarvis, You can get the Also and ...READ MORE

answered Jun 6, 2019 in Python by Kim
604 views
0 votes
1 answer

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 20, 2019 in Python by Noel Deepak Palle
5,918 views
–1 vote
2 answers
+2 votes
0 answers

I am trying to create mininet, but I cannot ping all host and switch

I am trying to do mininet environment ...READ MORE

Aug 4, 2019 in Python by Jiun-Jung
• 140 points
2,449 views
+1 vote
3 answers

While using pyttsx 3 , I am having the following ERROR, i have installed pypiwin32

Try this: pip uninstall pyttsx3 Then: pip install pyttsx3==2.71 READ MORE

answered Aug 12, 2020 in Python by Eghosa
7,967 views
0 votes
1 answer

Where can I find the error logs of nginx, using FastCGI and Django?

Hello @kartik, Errors are stored in the nginx ...READ MORE

answered Aug 6, 2020 in Python by Niroj
• 82,840 points
1,241 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP