调用输入时出错(“输入句子:”)[重复]

问题描述 投票:-4回答:1

这个问题在这里已有答案:

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 python-3.x python-2.7 input syntax-error
1个回答
1
投票

使用Python 2执行Python 3代码会导致此错误。

If you want to use Python 3

使用Python 3执行时,您的程序可以正常使用。只需使用python3命令运行它:

python3 you_code.py

If you want to use Python 2

  1. 删除print()的括号:if sentence == "have a nice day":
  2. input()替换raw_input()sentence = raw_input("Enter the sentence: ")

这给出了这个Python 2程序:

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
© www.soinside.com 2019 - 2024. All rights reserved.