-4

I need some help with my hangman game, whenever I put in a word like "gas" the game works fine but when I type in a word like "pool" it doesn't work. So I can't win if the word has two same letters. If you could help me that would be much appreciated.

Here is the code:

#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

Is there any way to search for duplicate letters?

4

1 回答 1

4

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

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

于 2012-11-08T22:36:10.357 回答