Python 在刽子手游戏中增加一个字母的限制

Python 在刽子手游戏中增加一个字母的限制,python,Python,在创建hangman游戏时,我意识到用户可以一次使用多个字母,而只使用一次生命。但是,我不知道在哪里/如何制作,因此如果程序有多个字母,则拒绝猜测。有什么帮助吗 import time print('Lets play hangman') print(""" _______ | | | O | | | \- -/ | | | / \\ | / \\

在创建hangman游戏时,我意识到用户可以一次使用多个字母,而只使用一次生命。但是,我不知道在哪里/如何制作,因此如果程序有多个字母,则拒绝猜测。有什么帮助吗

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()
导入时间
打印(‘让我们扮演刽子手’)
打印(“”)
_______
|    |
|O
|    |
|  \- -/
|    |
|   / \\
|  /   \\
____|____
\(以“)号填列)
打印(“”)
你有7条命去猜单词中的字母。如果你猜的话
更多的刽子手将出现,你将失去一条生命
用了你的7条命,那人会被绞死,你会输。如果你猜一个
正确的字母,它将显示,如果你不使用7猜单词
转身,你赢了!
\(以“)号填列)
easy_words=['string','loop','python','print','run']
中文字=[“模块”、“输入”、“逻辑”、“输出”]
硬词=[“图形窗口”、“变量”、“迭代”、“模块”]
random_words=['string','loop','python','print','run','graphics window','variable','iteration','modules','module','input','logic','output']
时间。睡眠(2)
打印(“您希望在哪个级别玩?简单、中等、硬或随机?”)
难度等级=输入()
时间。睡眠(2)
打印(“程序正在生成您的单词…”)
如果难度等级=‘容易’:
生成的单词=随机选择(简单单词)
elif难度等级=‘中等’:
生成的单词=随机选择(中等单词)
elif难度等级=‘难’
生成的单词=随机选择(硬单词)
elif难度等级==“随机”:
生成的单词=随机。选择(随机单词)
其他:
生成的单词=随机。选择(随机单词)
猜测=“”
生命=7
当生命>0时:
未命中=0
打印()
对于生成的单词中的字母:
如果猜中字母:
打印(字母,结尾=“”)
其他:
打印(“”,结束=“”)
未接=未接+1
如果未命中==0:
打印('\n\n您赢了!')
退出
打破
猜=输入(“请猜一个字母:”)
猜到的=猜到的+猜到的
如果猜测不在生成的单词中:
打印(“\n该字母不在此单词中,您的生命已减少1。”)
打印('请尝试另一封信')
生命=生命-1
未接=未接+1
打印('您的新生命值为')
印刷(生活)
如果寿命<7:
打印(“”)_______
|    |   ''')
如果寿命小于6:
打印(“| O”)
如果寿命小于5:
打印(“| |”)
如果寿命小于4:
打印(“| \-/”)
如果寿命小于3:
打印(“| |”)
如果寿命小于2:
打印(“|/\ \”)
如果寿命<1:
打印(“|/\ \”)
如果寿命=0:
印刷品
打印('游戏结束!你已经耗尽了生命,输掉了游戏')
print('秘密单词是')
打印(生成的单词)
退出

在提问时,我无法访问我的代码,所以我必须复制/粘贴我以前的问题。这意味着我的代码中可能有几个错误。抱歉。

将猜测输入更改为:

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

将猜测输入更改为:

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

要仅接受一个字母,并在多个字母的情况下警告用户,请执行以下操作:

guess = ""

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

要仅接受一个字母,并在多个字母的情况下警告用户,请执行以下操作:

guess = ""

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

如果要避免有人键入大写字母,请将输入行更改为:
guess=input(“请猜测字母:”).lower()
;如果要避免有人键入大写字母,请将输入行更改为:
guess=input(“请猜测字母:”).lower()