当有重复的字母时,python中的Hangman游戏不起作用

时间:2012-11-08 22:28:47

标签: python

我需要一些帮助我的刽子手游戏,每当我输入像“气体”这样的词时,游戏工作正常但是当我输入像“游泳池”这样的词时它不起作用。如果这个词有两个相同的字母,我就无法获胜。如果你能帮助我,我将非常感激。

以下是代码:

#Hangman
#By Justin 
#Last Revised Nov 8th 2012
#Plays a game of hangman with the user(s)

import time
import os
import sys
import random

gallows1='''
  '------'
  |      |
  |
  |
  |
  |
  |   
  |
  |
  |
  |
=================='''
gallows2='''
  '------'
  |      |
  |      O
  |      |
  |
  |
  |   
  |
  |
  |
  |
=================='''
gallows3='''
  '------'
  |      |
  |      O
  |     /|
  |
  |
  |   
  |
  |
  |
  |
=================='''
gallows4='''
  '------'
  |      |
  |      O
  |     /|\\
  |
  |
  |   
  |
  |
  |
  |
=================='''
gallows5='''
  '------'
  |      |
  |      O
  |     /|\\
  |     /
  |
  |   
  |
  |
  |
  |
=================='''
gallows6='''
  '------'
  |      |
  |      O
  |     /|\\
  |     / \\
  |
  |   
  |
  |
  |
  |
=================='''
gallowsDead='''
  '------'
  |      |
  |      |
  |      |
  |      O
  |     |||
  |     | |
  |
  |   
  |
  |
=================='''
gallowsAlive='''
  '------'
  |      |
  |      
  |      
  |                        _____________________
  |                       |                     |
  |                       |    Good Job :D      |
  |                       |                     |
  |                      <|_____________________|                    
  |                    O/ 
  |                   /|
==================    / \    '''
print"""
          _    _                                         
         | |  | |                                        
         | |__| | __ _ _ __   __ _ _ __ ___   __ _ _ __  
         |  __  |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \      
         | |  | | (_| | | | | (_| | | | | | | (_| | | | |
         |_|  |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                              _ / |                      
                             |___/                          
"""
print "                              MENU     "
print "                        *=============*"
print "                         1 Player Game"
print "                        *=============*"
print "                         2 Player Game"
print "                        *=============*"
print "                         Options      "
print "                        *=============*"
print "                         Exit         "
print "                        *=============*"
mode = ""
while mode != "2" or "1" or "O" or "E":
    mode=raw_input("Please type in your choice ")
    break
os.system("CLS")
if mode =="2":
    tries = 0
    usedLetters=""
    lettersCorrect=0
    originalWord=raw_input("A Two player game of Hangman it is, Player A please enter a word! ")
    os.system("CLS")
    wordLength=len(originalWord)
    solvedWord=wordLength*["_ "]
    maxTries=6
    while originalWord != solvedWord and maxTries <= 6:
        if tries == 0:
            print gallows1
        if tries == 1:
            print gallows2
        if tries == 2:
            print gallows3
        if tries == 3:
            print gallows4
        if tries == 4:
            print gallows5
        if tries == 5:
            print gallows6
        print "Player B,Try to guess the word!"
        print                solvedWord
        print
        print"Used Letters:",usedLetters
        print
        letter= raw_input("Please Guess a letter! ")
        if len(letter)== 1:
            if usedLetters.find(letter) != -1:
                print "You already picked", letter
            else:
                usedLetters = usedLetters + letter
                index = originalWord.find(letter)
                if index == -1:
                    tries = tries+1
                    print "The letter",letter,"is not in the word!"
                else:
                    print"The",letter,"is in the word."
                    lettersCorrect=lettersCorrect+1
                    for rang in range(wordLength):
                        if letter == originalWord[rang]:
                            solvedWord[rang] = letter
                    if 
        os.system("CLS")
        if tries == maxTries:
            print gallowsDead
            print "I'm sorry, Player B that was 6 guesses, You Lose."
            print 'The word was "',originalWord,'"'
            break
        if lettersCorrect == wordLength:
            print gallowsAlive
            print "Congratulations,Player B ,You win."
            print 'The word was "',originalWord,'"'
            break

有没有办法搜索重复的字母?

1 个答案:

答案 0 :(得分:4)

使用find意味着您只检查出现在originalWord中的第一个字母实例。所以对于“池”,猜测“o”应该将lettersCorrect增加2,但它只增加1。

尝试使用str.count(sub[, start[, end]])来计算被猜测的字母的实例总数,然后使用该数字以正确的数量增加lettersCorrect。