Python Forum
Translating language in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Translating language in python (/thread-2673.html)

Pages: 1 2


Translating language in python - Liquid_Ocelot - Apr-01-2017

Hey everyone.

I am struggling to get my head around nested if statements particularly with a program I am needing to write which requires me to use nested if-statements (not lists) in order to write a function that finds and prints the translation of either cat or dog from English into German, French or Spanish.

My code is looking like it is in the complete wrong direction 

word = ''
french = ''
print ("What do you want to translate to")
word = input()
while input != french:
    print ("Enter a word in french")
    break
while input == french:
    print ("Try again")
while word != 'dog':
    print ('Invalid')
    word = input()
while word == 'dog':
    print("chien")
    break
and the output 

What do you want to translate to
french
Enter a word in french
Invalid
dog
chien

Process finished with exit code 0



RE: Translating language in python - alicarlos13 - Apr-01-2017

You do not need to initialize anything in you are just looking for a simple translation. And if all you want is IF-statements. This is a quick one:

language = raw_input("What do you want to translate to :")
words = raw_input("Please state the word :")
if language == "French":
    if words == "chien":
        print("English => Dog")
        print("Dutch => hond")
    elif words == "chat":
        print("English => Cat")
        print("Dutch => kat")
    else:
        print("I can not understand")
    # KEEP ADDING ELIF FOR ALL THE OTHER WORDS
if language == "English":
    if words == "dog":
        print("French => chien")
        print("Dutch => hond")
    elif words == "cat":
        print("French => chat")
        print("Dutch => kat")
    # KEEP ADDING ELIF FOR ALL THE OTHER WORDS
    else:

        print("I can not understand")
If you want to go extreme you can add more if statements in case the user enters something wrong and/or enters a language that is not supported.


RE: Translating language in python - Liquid_Ocelot - Apr-01-2017

(Apr-01-2017, 09:17 PM)alicarlos13 Wrote: You do not need to initialize anything in you are just looking for a simple translation. And if all you want is IF-statements. This is a quick one:
language = raw_input("What do you want to translate to :") words = raw_input("Please state the word :") if language == "French":     if words == "chien":         print("English => Dog")         print("Dutch => hond")     elif words == "chat":         print("English => Cat")         print("Dutch => kat")     else:         print("I can not understand")     # KEEP ADDING ELIF FOR ALL THE OTHER WORDS if language == "English":     if words == "dog":         print("French => chien")         print("Dutch => hond")     elif words == "cat":         print("French => chat")         print("Dutch => kat")     # KEEP ADDING ELIF FOR ALL THE OTHER WORDS     else:         print("I can not understand") 
If you want to go extreme you can add more if statements in case the user enters something wrong and/or enters a language that is not supported.

Ah I see where I was going wrong. That gives me a basis to work on thanks heaps.


RE: Translating language in python - wavic - Apr-01-2017

Well, I would use a different approach.

french = {"chien": ("English => Dog", "Dutch => hond"),
               "chat": ("English => Cat", "Dutch => kat")}

english = {"dog": ("French => chien", "Dutch => hond"),
                 "cat": ("French => chat", "Dutch => kat")}

lang = input("Translate to - English/French: ")
word = input("Write a word: ")

if lang.lower() == french and word in french:
    for trans in french[word]:
        print(trans)
elif lang.lower() == english and word in english:
    for trans in english[word]:
        print(trans)
else:
    print("Not in the dictionary!")



RE: Translating language in python - Liquid_Ocelot - Apr-01-2017

So just for clarification in terms of the instructions:

"Using nested if-statements (not lists), write a function that finds and prints the translation of
either cat or dog from English into German, French or Spanish."

Would this be within the guidelines?

translate = input("What language do you want to translate? French, German or Spanish:")
words = input("Please state the word :")
if translate == "French":
   if words == "cat":
       print("The translation of cat into French is 'chat'")
   elif words == "dog":
       print("The translation of dog into French is 'chien'")
   else:
        print("I can not understand")

if translate == "German":
   if words == "dog":
       print("The Translation of dog into German is 'hond'")
   elif words == "cat":
       print ("The translation of cat into German is 'katze'")
   else:
       print("I can not understand")

if translate == "Spanish":
   if words == "dog":
       print("The translation of dog into Spanish is 'perro'")
   elif words == "cat":
       print ("The translation of cat into Spanish is 'gato'")
   else:
       print("I cannot understand")



RE: Translating language in python - sparkz_alot - Apr-01-2017

Yes, but the OP said they can only use "requires me to use nested if-statements (not lists) in order to write a function".  I presume that means dictionary's as well :-).  Though alicarlos13's answer fits the nested "if"'s  it's not really a function, though that criteria might not mean what it implies.  If it does mean what it says, it would be a simple matter to convert to a function.


RE: Translating language in python - Liquid_Ocelot - Apr-01-2017

(Apr-01-2017, 10:13 PM)sparkz_alot Wrote: I presume that means dictionary's as well :-).

Yup no dictionaries either :)


RE: Translating language in python - alicarlos13 - Apr-01-2017

(Apr-01-2017, 10:13 PM)sparkz_alot Wrote: "requires me to use nested if-statements (not lists) in order to write a function".  I presume that means dictionary's as well :-).
 I was just about to comment that. It very confusing, i mean are "lists" considered "tuples"/"dictionaries"; Guess the context of the question is not really clear.

@
Quote:Liquid_Ocolet
if translate == "German":
   if words == "dog":
       print("The Translation of dog into German is 'hond'")
   elif words == "cat":
       print ("The translation of cat into German is 'katze'")
   else:
       print("I can not understand")
There is something not correct here, so the users input "GERMAN" language and is supposed to enter the words in "ENGLISH".

And converting it into a simple function


def trans(language, words):
    if language == "French":
        if words == "chien":
            print("English => Dog")
            print("Dutch => hond")
        elif words == "chat":
            print("English => Cat")
            print("Dutch => kat")
        else:
            print("I can not understand")
        # KEEP ADDING ELIF FOR ALL THE OTHER WORDS
    if language == "English":
        if words == "dog":
            print("French => chien")
            print("Dutch => hond")
        elif words == "cat":
            print("French => chat")
            print("Dutch => kat")
        # KEEP ADDING ELIF FOR ALL THE OTHER WORDS
        else:
            print("I can not understand")

language = raw_input("What do you want to translate to :")
words = raw_input("Please state the word :")
trans(language, words)



RE: Translating language in python - Liquid_Ocelot - Apr-01-2017

(Apr-01-2017, 10:24 PM)alicarlos13 Wrote:  I was just about to comment that. It very confusing, i mean are "lists" considered "tuples"/"dictionaries"; Guess the context of the question is not really clear.

yeah its thrown me off too.

I guess as long as I am getting the user to

1 select a language to be translated
2 enter either dog or cat
3 then print the translated word in chosen language from english

I think I am on the right track.

Also a footnote does state If you use and in the if conditions, an alternative solution could use just the if-elif construct, without
any nested if statements.


RE: Translating language in python - sparkz_alot - Apr-01-2017

OK, alicarlos13's last post meets all the criteria of the original post (well sort of  Wink), you will have to tinker a bit with the statements in order to get the output you want, but the structure is there as requested...nested 'if' statements contained within a function.

Quote:@alicarlos13 wrote:
 so the users input "GERMAN" language and is supposed to enter the words in "ENGLISH". 

yes, I think.  "words" is cat or dog in English and "language" is the equivalent output in  either French, German or Spanish.