2016-11-14 112 views
0

對於我的計算科學控制的評估,我被要求用Python創建一個Hang子手遊戲。但是,在創建遊戲時,出現了一條錯誤消息,指定了不在我的代碼中的東西。Python Hang子手代碼

Traceback (most recent call last): 
File "Z:\Documents\hangman.py", line 40, in <module> 
generated_word = random.choice(random) 
File "Q:\Pywpygam.001\Python3.2.3\lib\random.py", line 249, in choice 
i = self._randbelow(len(seq)) 
TypeError: object of type 'module' has no len() 

我沒有在代碼中看到任何錯誤,也沒有指定錯誤。這裏是我的代碼:

import time 
print('Lets play hangman') 
print(""" 
     _______ 
     | | 
     | O 
     | | 
     | \- -/ 
     | | 
     | /\\ 
     |/ \\ 
    ____|____ 
    \n""") 

print(""" 
you get 7 lives to guess the letters in the word. if you guess 
incorrectly more of the hangman will appear and you lose a life. when you have 
used your 7 lives, the man will have hung and you will have lost. If you guess a 
correct letter, it will be show and if you guess the word without using the 7 
turns, you win! 
\n""") 

easy_words = ['string', 'loop', 'python', 'print', 'run'] 
medium_words = ['module', 'input', 'logic', 'output'] 
hard_words = ['graphics window', 'variable', 'iteration', 'modules'] 
random_words = ['string', 'loop', 'python', 'print', 'run', 'graphics window', 'variable', 'iteration', 'modules', 'module', 'input', 'logic', 'output '] 
time.sleep(2) 
print ("What level would you like to play at? Easy, Medium or Hard or Random?") 
level_of_difficulty = input() 
time.sleep(2) 
print ("The program is now generating your word...") 
if level_of_difficulty == 'Easy': 
    generated_word = random.choice(easy_words) 
elif level_of_difficulty == 'Medium': 
    generated_word = random.choice(medium_words) 
elif level_of_difficulty == 'Hard': 
    generated_word = random.choice(hard_words) 
elif level_of_difficulty == 'Random': 
    generated_word = random.choice(random_words) 
else: 
    generated_word = random.choice(random_words) 
guessed = '' 

lives = 7 


while lives > 0: 

    missed = 0 
    print() 
    for letter in generated_word: 
     if letter in guessed: 
      print (letter,end=' ') 
     else: 
      print ('_',end=' ') 
      missed = missed + 1 
    if missed == 0: 
     print ('\n\nYou win!') 
     quit() 
     break 
    guess=input("please guess a letter:") 

    guessed = guessed + guess 
     if guess not in generated_word: 

      print ('\n that letter is not in this word, your lives have been decreased by 1.') 
     print ('please try another letter.') 
     lives = lives - 1 
     missed = missed + 1 

     print('your new lives value is') 

     print(lives) 

     if lives < 7: 
      print(''' _______ 
    | | ''') 
      if lives < 6: 
       print(' | O ') 
      if lives < 5:    
       print(' | | ') 
      if lives < 4: 
       print(' | \- -/ ') 
      if lives < 3: 
       print(' | | ') 
      if lives < 2: 
       print(' | /\\ ') 
      if lives < 1: 
       print(' |/ \\ ') 
      if lives == 0: 
      print('___|___  ') 

      print('GAME OVER! You have run out of lives and lost the game') 
      print('the secret word was') 
      print(generated_word) 
      quit() 

我所要做的是使它這樣生成的字顯示在窗口中出現了_,當信猜中,適當_充滿信。 任何想法將不勝感激!

+5

我假設你的意思是'random.choice(random_words)'。 – polku

+0

我沒有看到,該死的 – Lucas

+0

我真是個白癡,我值得被槍殺。謝謝你,polku – Lucas

回答

0

我覺得這些線是錯誤的:

generated_word = random.choice(random) 

你傳入模塊「隨機」作爲參數。我想你應該通過變量「random_words」?

+0

實際上,我修正它,它仍然無法正常工作? – Lucas

0

我看到我做錯了什麼,感謝您指出的

generated_word = random.choice(random) 

一點,但我也看到,在

while lives > 0: 

    missed = 0 
    print() 
    for letter in generated_word: 
     if letter in guessed: 

我已經做了拼寫錯誤。 謝謝大家!