2016-12-30 50 views
1

创建hang子手游戏后,我意识到用户可以一次只使用一个以上的字母,而只使用一次生命。但是,我不知道在何处/如何制作它,因此程序拒绝猜测,如果它有多个字母。任何帮助?向hang子手游戏添加一个字母限制

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() 

在提问时,我没有访问我的代码,所以我不得不从我以前的问题中复制/粘贴。这意味着我的代码中可能会有一些错误。道歉。

回答

7

你的猜测输入更改为:

guess = "" 
while len(guess) != 1: 
    guess=input("please guess a letter:") 
+0

,如果你想避免情况下,某人输入一个大写字母,将输入行更改为:'guess = input(“请猜测一个字母:”)。lower()' – freginold

2

接受只有一个字母,并警告在多个字母的情况下,用户做到以下几点:

guess = "" 

while len(guess) != 1: 
    guess=input("please guess a letter:") 
    if len(guess) != 1: 
     print("Please guess only one letter.")