2014-11-25 65 views
0

当我尝试运行代码时,我不断收到EOF错误。我想知道如何解决这个问题?在此先感谢。当我尝试删除if_name_时,该程序没有任何语法错误但未运行。Hang子手Python错误

这里是一个链接,显示错误:codepad.org/d5p1Mgbb

def get_secret_word(): 

while True: 

secret_word = input('Please enter a word to be guessed\nthat does not contain ? or whitespace:') 

if not ('?' in secret_word or ' ' in secret_word or secret_word == ''): 

      return secret_word 

def is_game_over(wrong_guess,secret_word,output,chars_guessed): 

    if wrong_guess == 7: 

    print('You failed to guess the secret word:',secret_word) 

     return True 

    for guess in secret_word: 

    if guess in chars_guessed: 
     output += guess 

else: 

    output += '?' 

if not('?' in output): 

print('You correctly guessed the secret word:',secret_word) 

return True 

    else: 

    return False 

    def display_hangman(wrong_guess): 

    if wrong_guess == 1: 

    print('\n |') 

    elif wrong_guess == 2: 

    print('\n |','\n 0') 

    elif wrong_guess == 3: 

    print('\n |','\n 0','\n |') 

    elif wrong_guess == 4: 

    print('\n |','\n 0','\n/|') 

    elif wrong_guess == 5: 

    print('\n |','\n 0','\n/|\\') 

    elif wrong_guess == 6: 

    print('\n |','\n 0', '\n/|\\','\n/') 

    elif wrong_guess == 7: 

    print('\n |','\n 0','\n/|\\','\n/','\\') 

    def display_guess(secret_word,chars_guessed): 

     print("") 

     output = '' 

for guess in secret_word: 

if guess in chars_guessed: 

    output += guess 

else: 

    output += '?' 

if '?' in output: 

output += '\nSo far you have guessed: ' 

for guess in chars_guessed: 

output += guess + "," 

print(output.strip(",")) 

def get_guess(secret_word,chars_guessed): 

while True: 

guess_letter = input('Please enter your next guess: ') 

if guess_letter == '': 

print('You must enter a guess.') 

continue 

elif len(guess_letter) > 1: 

print('You can only guess a single character.') 

elif guess_letter in chars_guessed: 

print('You already guessed the character:',guess_letter) 

else: 

return guess_letter 

def main(): 

wrong_guess = 0 

chars_guessed = [] 

secret_word = get_secret_word() 

output='' 

while not(is_game_over(wrong_guess,secret_word,output,chars_guessed)): 
    display_hangman(wrong_guess) 
    display_guess(secret_word, chars_guessed) 
    guess_character = get_guess(secret_word, chars_guessed) 
    chars_guessed.join(guess_letter) 
    chars_guessed.sort() 
    if not guess_letter in secret_word: 
     wrong_guess += 1 
return wrong_guess 
pass 


if __name__ == '__main__': 

main() 
+2

你将不得不在这里修复你的代码块。这对于我(或其他人)来说很适合猜测正确的缩进 – Andy 2014-11-25 03:20:44

+0

我不知道如何修复缩进,但我添加了一个显示错误的链接。 – DatOneGuy 2014-11-25 03:28:31

+2

阅读[this](http://stackoverflow.com/help/formatting)用于格式化帮助 – Andy 2014-11-25 03:30:04

回答

0

有几件事情,我发现...

  • guess_characterguess_letter似乎是相同的变量,但guess_letter在定义之前使用。
  • input允许输入变量名或任何Python代码块。为了将输入用作字符串,您必须强制输入字符串或(更容易)使用raw_input
  • 您试图对列表执行join操作。这是不可能的。不过,您可以将项目添加到列表append

所以,这里的代码有一些修复。你将不得不修复打印循环,但它应该会更好,并让你通过以前的错误。

def get_secret_word(): 
    while True: 
     secret_word = raw_input('Please enter a word to be guessed\nthat does not contain ? or whitespace:') 
     if not ('?' in secret_word or ' ' in secret_word or secret_word == ''): 
      return secret_word 
def is_game_over(wrong_guess,secret_word,output,chars_guessed): 
    if wrong_guess == 7: 
     print('You failed to guess the secret word:',secret_word) 
     return True 
    for guess in secret_word: 
     if guess in chars_guessed: 
      output += guess 
     else: 
      output += '?' 
     if not('?' in output): 
      print('You correctly guessed the secret word:',secret_word) 
      return True 
     else: 
      return False 
def display_hangman(wrong_guess): 
    if wrong_guess == 1: 
     print('\n |') 
    elif wrong_guess == 2: 
     print('\n |','\n 0') 
    elif wrong_guess == 3: 
     print('\n |','\n 0','\n |') 
    elif wrong_guess == 4: 
     print('\n |','\n 0','\n/|') 
    elif wrong_guess == 5: 
     print('\n |','\n 0','\n/|\\') 
    elif wrong_guess == 6: 
     print('\n |','\n 0', '\n/|\\','\n/') 
    elif wrong_guess == 7: 
     print('\n |','\n 0','\n/|\\','\n/','\\') 

def display_guess(secret_word,chars_guessed): 
    print("") 
    output = '' 
    for guess in secret_word: 
     if guess in chars_guessed: 
      output += guess 
     else: 
      output += '?' 
      if '?' in output: 
       output += '\nSo far you have guessed: ' 
       for guess in chars_guessed: 
        output += guess + "," 
        print(output.strip(",")) 
def get_guess(secret_word,chars_guessed): 
    while True: 
     guess_letter = raw_input('Please enter your next guess: ') 
     if guess_letter == '': 
      print('You must enter a guess.') 
      continue 
     elif len(guess_letter) > 1: 
      print('You can only guess a single character.') 
     elif guess_letter in chars_guessed: 
      print('You already guessed the character:',guess_letter) 
     else: 
      return guess_letter 
def main(): 
    wrong_guess = 0 
    chars_guessed = [] 
    secret_word = get_secret_word() 
    output='' 

    while not(is_game_over(wrong_guess,secret_word,output,chars_guessed)): 
     display_hangman(wrong_guess) 
     display_guess(secret_word, chars_guessed) 
     guess_character = get_guess(secret_word, chars_guessed) 
     chars_guessed.append(guess_character) 
     chars_guessed.sort() 
     if not guess_character in secret_word: 
      wrong_guess += 1 
    return wrong_guess 
    pass 


if __name__ == '__main__': 
    main() 
+0

我得到一个错误,说原始输入未定义以及secret_word的问题。 – DatOneGuy 2014-11-25 03:56:08

+0

粘贴确切的错误。你正在运行Python 2.7或其他版本?需要更多信息或无法帮助。 – 2014-11-25 04:00:14

+0

我正在运行Python 3 – DatOneGuy 2014-11-25 04:02:41