Python 调用输入时出错(“输入句子:”)
我在句子=输入句子时出错: 此错误是由使用Python2执行Python3代码引起的 如果您想使用Python3 当使用Python 3执行时,您的程序对我来说运行良好。只需使用python3命令即可运行它:Python 调用输入时出错(“输入句子:”),python,python-3.x,python-2.7,input,syntax-error,Python,Python 3.x,Python 2.7,Input,Syntax Error,我在句子=输入句子时出错: 此错误是由使用Python2执行Python3代码引起的 如果您想使用Python3 当使用Python 3执行时,您的程序对我来说运行良好。只需使用python3命令即可运行它: Traceback (most recent call last): File "Lab3.py", line 3, in <module> sentence = input("Enter the sentence: ") File "<string>
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
如果您想使用Python2
删除print的括号:如果句子==祝您愉快:
用原始输入替换输入:句子=原始输入输入输入句子:
这为Python 2程序提供了:
python3 you_code.py
您遇到了什么错误?请编辑问题的完整错误回溯。您使用的是Python 2还是Python 3?如果您使用的是Python 2,那么应该使用raw_input@Barmar:考虑到结尾的打印有括号,我猜它应该是Python 3。但不能确定。请尝试使用python3运行代码。
python3 you_code.py
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