Python Forum
Translating language in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translating language in python
#1
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
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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!")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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")
Reply
#6
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Apr-01-2017, 10:13 PM)sparkz_alot Wrote: I presume that means dictionary's as well :-).

Yup no dictionaries either :)
Reply
#8
(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)
Reply
#9
(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.
Reply
#10
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  American Adolescent language in python. Deo 2 1,450 Feb-18-2022, 12:12 PM
Last Post: perfringo
  Translating orderflow into code reez0105 0 1,234 Nov-16-2019, 05:28 AM
Last Post: reez0105

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020