简体   繁体   中英

Coding a language translator

im trying to code a kind of language translator from any language to a kind of gibberish language, wheres every consonant will be replaced with the same consonant plus an o and then the consonant again.

b = bob
d = dod
f = fof

so the text "Hi my name is x" will become "Hohi momy nonamome isos xox"

The problem i have is the converting part.

any tips on how i can proceed?

Oh and btw I am using python 3

What i got this far.

#Welcom text
print ("Gibberish translator!")

#get stentence
original = raw_input("Give a sentence: ")

#Check so that it is a correct sentence
if len(original) > 0:
    print ("")
else:
    print ("give a real sentence..: ")

#convert
gibberish = ""
for i in original:
    if i == "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,z":
        i = i + "0" + i
        gibberish.append(i)
    elif i == "a,o,u,e,i,y":
        gibberish.append(i)

#print out the gibberish
print (gibberish)

Yeah! i think i got it to work quite well..

# -*- coding: cp1252 -*-
#Repeat
while True :
    #Welcom text
    print ("Gibberish translator!")

    #get stentence
    original = raw_input("Give a sentence: ")

    #Check so that it is a correct sentence
    if len(original) > 0:
        print ("")
    else:
        print ("Give a real sentence..: ")

    #convert
    gibberish = ""
    for i in original:
        if i in "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ":
            i = i + "o" + i
            gibberish = gibberish + i
        elif i in "aoueiyåäö AOUEIYÅÄÖ":
            gibberish = gibberish + i

    #print out the gibberish
    print (gibberish)
    print ("\n")

Im open for suggestions to make it "better"

The problem is you're comparing a character i to a string "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,z" . The two will never be equal.

What you want to do instead is use the in operator.

    if i in 'bcdfghjklmnpqrstvwxz':

Also, strings don't have an .append() method, only lists do.
You can create a string from a list of strings by doing ''.join(my_list)

If and in statements don't work like that. This is actually a very common mistake, so no worries. When you get to the if i == "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,z": python reads that as "if i is all of this string (the string that contains all the consonants). Now unless you enter that string exactly somewhere in your sentence, python is going to think "nope no string like that" and skip it. You have the same problem with your vowel statements.

For the fastest fix:

if i in "bcdfghjklmnpqrstvwxz": #note the removal of commas, don't want them getting "o'd"
    #rest of your code for consonants
else: #having an else is often very good practice, or else you may not get a result.
    #what to do if its not a consonant

The function checks if it is a lower case vowel (might want to add stuff for upper case letters), then if it isn't, it checks if it is a letter period. string is very useful when working with strings. You should look at the docs .

And finally, you need to change append to just use + with strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM