Hangman语法错误无效

时间:2016-05-08 05:26:32

标签: python syntax-error

我一直收到无效的语法错误,并突出显示代码行的单词='动物' .split()我的代码看起来正确我不知道造成这种情况的原因请帮忙

import random
HANGMANPICS = ['''


  ''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()

def getRandomWord(wordList):

    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]

3 个答案:

答案 0 :(得分:1)

我已经执行了你的代码(使用python 2.7.11)并且我没有遇到任何错误。

我也在http://www.tutorialspoint.com/execute_python_online.php执行代码 它工作正常。

我已添加" print getRandomWord(words)"对你的代码,所以当我执行它时,我每次都有不同的动物。

请尝试复制/粘贴我的版本并在在线服务器中执行:

import random
HANGMANPICS = ['''


  ''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()

def getRandomWord(wordList):

    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]

print getRandomWord(words)

答案 1 :(得分:0)

我也没有收到任何错误(Windows上的python2.7.11)。此外,您可能希望在"单词"中使用三重引号。这样你就可以将线长缩小到更方便的水平。

import random
HANGMANPICS = ['''


 ''']
words = '''ant baboon badger bat bear beaver camel cat clam cobra
cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose 
hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda 
parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep 
skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel
whale wolf wombat zebra'''.split() 

def getRandomWord(wordList):

    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]

 test = getRandomWord(words)
 print test

答案 2 :(得分:0)

好的,我得到了它,我输入''','''而不是''','''我猜python对此有所疑问。转到下一条错误消息。