-4
import random

sentence = input("Enter the sentence: ")
sentence=sentence.lower()
languages=[['hello','goodbye','thank you',"you\'re welcome",'have','a','nice','day','how','are','you'],
               ['ola','Tehau','obrigado','seja bem-vindo','ter','uma','bom','dia','como','esta','voce'],
               ['hello','faida','Asante','karibu','kuwa na','a','nzuri','siku','vipi','ni','wewe'],
               ['hallo','Vaarwel','dank je','graag gedaan','habben','een','leuk','dag','hoe','ziin','u'],
               ['hola','adios','gracias','De nada','tener','un','bonito','dia','como','son','tu']]
t=''

if sentence == "have a nice day":
    words=sentence.split()
    for word in words:
        i=languages[0].index(word)
        r=random.randint(1,4)
        t+=languages[r][i]+' '
elif sentence == "goodbye":
    i=languages[0].index(sentence)  
    r=random.randint(1,4)   
    t+=languages[r][i]
elif sentence == "hello":
    i=languages[0].index(sentence)
    r=random.randint(1,4)  
    t+=languages[r][i]
elif sentence == "you're welcome":
    i=languages[0].index(sentence) 
    r=random.randint(1,4)
    t+=languages[r][i]
elif sentence== "thank you":
    i=languages[0].index(sentence)
    r=random.randint(1,4)
    t+=languages[r][i]
elif sentence== "how are you":
    words=sentence.split()
    for word in words:
        i=languages[0].index(word)
        r=random.randint(1,4)
        t+=languages[r][i]+' '

print("The translated sentence is",t)

I got this error on sentence=input("Enter the sentence"):

Traceback (most recent call last):
  File "Lab3.py", line 3, in <module>
    sentence = input("Enter the sentence: ")
  File "<string>", line 1
    have a nice day
         ^
SyntaxError: invalid syntax
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
ruby
  • 9

1 Answers1

1

This error is caused by executing Python 3 code with Python 2.

If you want to use Python 3

Your program works fine for me when executed with Python 3. Just use the python3 command to run it:

python3 you_code.py

If you want to use Python 2

  1. Remove print()'s parenthesis: if sentence == "have a nice day":
  2. Replace input() by raw_input(): sentence = raw_input("Enter the sentence: ")

Which gives this Python 2 program:

import random

sentence = raw_input("Enter the sentence: ")
sentence=sentence.lower()
languages=[['hello','goodbye','thank you',"you\'re welcome",'have','a','nice','day','how','are','you'],
               ['ola','Tehau','obrigado','seja bem-vindo','ter','uma','bom','dia','como','esta','voce'],
               ['hello','faida','Asante','karibu','kuwa na','a','nzuri','siku','vipi','ni','wewe'],
               ['hallo','Vaarwel','dank je','graag gedaan','habben','een','leuk','dag','hoe','ziin','u'],
               ['hola','adios','gracias','De nada','tener','un','bonito','dia','como','son','tu']]
t=''

if sentence == "have a nice day":
    words=sentence.split()
    for word in words:
        i=languages[0].index(word)
        r=random.randint(1,4)
        t+=languages[r][i]+' '
elif sentence == "goodbye":
    i=languages[0].index(sentence)  
    r=random.randint(1,4)   
    t+=languages[r][i]
elif sentence == "hello":
    i=languages[0].index(sentence)
    r=random.randint(1,4)  
    t+=languages[r][i]
elif sentence == "you're welcome":
    i=languages[0].index(sentence) 
    r=random.randint(1,4)
    t+=languages[r][i]
elif sentence== "thank you":
    i=languages[0].index(sentence)
    r=random.randint(1,4)
    t+=languages[r][i]
elif sentence== "how are you":
    words=sentence.split()
    for word in words:
        i=languages[0].index(word)
        r=random.randint(1,4)
        t+=languages[r][i]+' '

print "The translated sentence is", t
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56