【发布时间】:2018-03-05 21:02:55
【问题描述】:
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)
我在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
【问题讨论】:
-
你遇到了什么错误?
-
请编辑完整的错误回溯到问题中。
-
您使用的是 Python 2 还是 Python 3?如果您使用的是 Python 2,则应使用
raw_input() -
@Barmar:鉴于末尾的
print有括号,我猜它应该是 Python 3。但不能确定。 -
尝试用 python3 运行你的代码。
标签: python python-3.x python-2.7 input syntax-error