-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 に表示される文字の最初のインスタンスのみをチェックしていることを意味します。したがって、「プール」の場合、「o」を推測すると、lettersCorrect が 2 増加するはずですが、1 だけ増加しています。

str.count (sub[, start[, end]])を使用して、推測されている文字のインスタンスの総数をカウントし、その数を使用して、lettersCorrect を適切な量だけインクリメントします。

于 2012-11-08T22:36:10.357 に答える