调用输入时出错("输入句子:")

时间:2018-03-05 21:02:56

标签: python python-3.x python-2.7 input syntax-error

CLLocationCoordinate2D

我在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")

1 个答案:

答案 0 :(得分: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: ")
  3. 这给出了这个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