【问题标题】:Error when calling input("Enter the sentence: ") [duplicate]调用输入时出错(“输入句子:”)[重复]
【发布时间】: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


【解决方案1】:

此错误是由使用 Python 2 执行 Python 3 代码引起的。

如果你想使用 Python 3

使用 Python 3 执行您的程序对我来说运行良好。只需使用 python3 命令运行它:

python3 you_code.py

如果你想使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多